重构 java 方法以降低圈复杂度

Refactoring java method to reduce cyclomatic complexity

    ArrayList cars = vehicles.getExtras();

    Iterator it = cars.iterator();
    while(it.hasNext()){
        Extra cExtra = (Extra)it.next();

            if (cExtra.getExtraId()==INSURANCE)
                insurance = cExtra.getExtra();

            if (cExtra.getExtraId()==ROAD_ASSISTANCE_CODE)
                roadAssCode = cExtra.getExtra();

            if (cExtra.getExtraId()==TYPE)
                replacementCarServiceType = cExtra.getExtra();

            if (cExtra.getExtraId()==CATEGORY)
                replacementCarCategory = cExtra.getExtra();

            if (cExtra.getExtraId()==ASSISTANCE){
                roadAssistanceCode = cExtra.getExtra();
            }

我还有很多这些 'if' 类型我没有包括在内。 我该如何重构它,从而降低圈复杂度。

Polymorphism is usually the key to reducing CC。如果你有一组相似的东西,它们做着相似但不同的事情,那么也许这些东西可以继承自一个共同的基础 class 但覆盖某个方法以提供不同的行为。

有时您可能会听到人们宣称您可以使用多态性来删除所有 if,但是通常在两个极端之间有一个很好的平衡。你通常想先努力满足 SOLID principles 然后较低的 CC 会来。

重构:改进现有代码的设计这本书是关于这个主题的一本很好的读物。显然Refactoring to Patterns也是,但我没看过。

这是一个示例,说明如何重构您的代码以获得 1 的圈复杂度(我不确切知道您的代码是如何设置的,但我想我重新创建了它的想法):

import java.util.ArrayList;
import java.util.List;

public class ExtraTest {
    public static void main(String[] whatsUp) {
        MyData myData = new MyData();

        List<Extra> myExtras = new ArrayList<>();
        myExtras.add(new ExtraInsurance("ReallyBadInsurance"));
        myExtras.add(new ExtraCarCategory(CarCategory.really_big_truck));

        System.out.println("Data before: " + myData);

        myExtras.forEach(extra -> extra.applyExtra(myData));

        System.out.println("Data after: " + myData);
    }

    public static enum CarCategory {not_a_truck, truck, big_truck, really_big_truck}

    public static class MyData {
        String insurance = "none";
        CarCategory carCategory = CarCategory.not_a_truck;

        @Override
        public String toString() {
            return insurance + " : " + carCategory.toString();
        }
    }

    public abstract static class Extra<T> {
        protected final T extraAttributeToProvide;

        public Extra(T extraAttributeToProvide) {
            this.extraAttributeToProvide = extraAttributeToProvide;
        }

        public abstract void applyExtra(MyData myData);
    }

    public static class ExtraInsurance extends Extra<String> {
        public ExtraInsurance(String extraAttributeToProvide) {
            super(extraAttributeToProvide);
        }

        public void applyExtra(MyData myData) {
            myData.insurance = extraAttributeToProvide;
        }
    }

    public static class ExtraCarCategory extends Extra<CarCategory> {

        public ExtraCarCategory(CarCategory extraAttributeToProvide) {
            super(extraAttributeToProvide);
        }

        public void applyExtra(MyData myData) {
            myData.carCategory = extraAttributeToProvide;
        }
    }

}

输出:

Data before: none : not_a_truck

Data after: ReallyBadInsurance : really_big_truck