0

For the code down below, SonarQube and ESLint calculate a cyclomatic complexity of 11. I calculated that too. However, the IntelliJ Linter calculates a cyclomatic complexity of 12. Is the IntelliJ Linter incorrect or exists there another way of calculating cyclomatic complexity?

I didn't find another way to calculate that metric online so I would tend to the former.

public test(array: []) {
    let startIdx = 0;
    while (startIdx < array.length -1) {
        let smallestIdx = startIdx;
        for (let i = startIdx + 1; i < array.length; i++) {
            if (array[smallestIdx] > array[i]) {
                smallestIdx = i;
            }
            HighCyclomaticComplexity.swap(startIdx, smallestIdx, array);
            startIdx++;
        }
    }
    let arrayEven = [];
    let arrayOdd = [];
    for (let element of array) {
        let parity;
        if (element % 2 === 0) {
            parity = 0;
        } else {
            parity = 1;
        }
        switch (parity) {
            case 0:
                arrayEven.push(element);
                break;
            case 1:
                arrayOdd.push(element);
                break;
            default:
                console.log('Unexpected error');
        }
    }
    if (arrayEven.length > arrayOdd.length) {
        return 'There are more even numbers';
    }
    if (arrayEven.length < arrayOdd.length) {
        return 'There are more odd numbers';
    }
    if (arrayEven.length === arrayOdd.length) {
        return 'Neither of them have the upper-hand';
    }
    return '';
}
Daumel
  • 1

0 Answers0