What is the difference between NPathComplexity and CyclomaticComplexity? If possible, for example, Java code. Stumbled upon these concepts, while digging into PMD Warnings.

    1 answer 1

    I'll try to explain. We have a method:

    private static boolean isSolder(int age, boolean isFemale) { boolean result; if (isFemale) { if (age < 18) result = false; else result = true; } else result = false; return result; } 

    To calculate it CyclomaticComplexity enough for each branch (if,while,for,case) add to the complexity + 1. As a result, we get 2. Another 1 must be added to declare the method itself. Total 3.

    With NPathComplexity bit more complicated. This metric shows how many paths there are in a program that can be executed. To make it clearer, we construct a graph.

    enter image description here

    Where:

    vertex 0 is an if (isFemale) condition if (isFemale)

    vertex 6 is an if (age < 18) condition if (age < 18)

    it is easy to calculate that there are only 3 acyclic paths in this column. This number will be the final estimate.

    • And if the method has several if , going in a row, will be taken into account the one with the highest complexity, or the sum of their difficulties (in each case)? - TheSN
    • As I understand it, they should be summed up - Artem Konovalov