Home > Net >  How i reduce cyclomative complexity of ths method
How i reduce cyclomative complexity of ths method

Time:01-03

private boolean checkForNameAndAmbigousInfo(final Attribute<NonSpatialObject> od,
        final Attribute<NonSpatialObject> od1) {

        if (isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination)
            && (isAmbigous(od) && isAmbigous(od1)))
            return true;
        if (isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination)
            && (!isAmbigous(od) && !isAmbigous(od1)))
            return true;
        if (isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination)
            && (isAmbigous(od) && !isAmbigous(od1)))
            return true;
        if (isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination)
            && (!isAmbigous(od) && isAmbigous(od1)))
            return true;

        return false;

    }

private boolean isAmbigous(final Attribute<NonSpatialObject> currSignInfo) {
        final DictionaryRangeItem ambiAttribute =
            getToolkit().getDictionaryTools().getDictionaryRangeItemFromComposite(currSignInfo,
                getModel().FEATURES.SignpostInfo.AmbiguousInfo);

        return ambiAttribute.equals(getModel().RANGES.AmbiguousInfo.Ambiguous);
    }

There is sonar lint issue with this method. the cyclometric complexity of this method is 13 which is greated than 10

authorized. i what should i have to do now.

CodePudding user response:

Provided isNameMatching has no side-effects:

boolean isNameMatching = isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination);
return (isNameMatching && (isAmbigous(od) && isAmbigous(od1)))
    || (isNameMatching && (!isAmbigous(od) && !isAmbigous(od1)))
    || (isNameMatching && (isAmbigous(od) && !isAmbigous(od1)))
    || (isNameMatching && (!isAmbigous(od) && isAmbigous(od1)));

The 2nd part of the condition seems like a bug. "Return true if both are unambiguous"? Seems wrong.

Probably can be

boolean isNameMatching = isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination);
return isNameMatching && (isAmbigous(od) || isAmbigous(od1));

CodePudding user response:

Your first operand is always identical and then you test for A&B, !A&!B, A&!B, and !A&B and return true in all cases. Without looking closer, I think your code can probably be reduced to return isNameMatching(od, od1, …) (because your ambiguous calls will always return true for at least one of the conditions). In mathematical logic that's called a tautology (a statement which is always true).

(Given that isNameMatching is idempotent and none of your methods have side-effects, of course).

How to come to that conclusion yourself? Extracting variables and converting to a single boolean expression might help:

    final boolean nameMatching = isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination);
    final boolean ambiguous1 = isAmbiguous(od);
    final boolean ambiguous2 = isAmbiguous(od1);
    return nameMatching && ambiguous1 && ambiguous2 
            || nameMatching && !ambiguous1 && !ambiguous2
            || nameMatching && ambiguous1 && !ambiguous2
            || nameMatching && !ambiguous1 && ambiguous2;

As you can see, at least one of the ambiguous checks must be true (you are checking all four possibilities: TT, TF, FT, FF). If at least one or-expression is true, then the final result can only be nameMatching && true, which can be further reduced to simply nameMatching.

If you are unsure about the reasoning about the or-expressions, the common term can be extracted:

    final boolean nameMatching = isNameMatching(od, od1, getModel().FEATURES.SignpostInfo.OtherDestination);
    final boolean ambiguous1 = isAmbiguous(od);
    final boolean ambiguous2 = isAmbiguous(od1);
    return nameMatching &&
             (  ambiguous1 &&  ambiguous2 
            || !ambiguous1 && !ambiguous2
            ||  ambiguous1 && !ambiguous2
            || !ambiguous1 &&  ambiguous2);

Now it should be clear that the result can only be equivalent to return nameMatching;.

  •  Tags:  
  • java
  • Related