I have a question regarding Cyclometric Complexity.
I'm looking to include a number of metrics in my Browse and Doc It plug-in and I've been researching the above (wikipedia and the metric notes in RAD Studio) and they mention IF statement and later on conditional loops but they are silent on CASE statements.
If an IF statement increases the complexity by 1 should a CASE statement increase the complexity by at least 1 if not more due to the number of branches?
I'm looking to include a number of metrics in my Browse and Doc It plug-in and I've been researching the above (wikipedia and the metric notes in RAD Studio) and they mention IF statement and later on conditional loops but they are silent on CASE statements.
If an IF statement increases the complexity by 1 should a CASE statement increase the complexity by at least 1 if not more due to the number of branches?
- Oct 9, 2017
- See mccabe.com - www.mccabe.com/pdf/mccabe-nist235r.pdf and the link from which I got this original source ...
https://stackoverflow.com/questions/30240236/cyclomatic-complexity-of-switch-case-statement
... as I understand it ...
McCabes CC counted case fallthroughs as 1 - and every case also as 1
So IMO ...
procedure TForm3.CC;
var
number: Integer;
begin
number := 17;
Case number mod 2 of
0:
ShowMessage(IntToStr(number) + ' mod 2 = 0');
1:
ShowMessage(IntToStr(number) + ' mod 2 = 1');
else
ShowMessage(IntToStr(number) + ' mod 2 is unknown');
end;
end;
... should have a CC of 3 - which it has not in Delphi ... where it has 2 ... which seems to be aligned with the more modern way of counting, where the fallthrough is not counted :-)Oct 9, 2017 - +Roland Kossow thank for the links my google foo last night and this morning completely fail. In your example the case statement has a CC of 3 (which I agree with) but the method 4 as an empty method should have a CC of 1. I’ll read the PDF.Oct 10, 2017
- +David Hoyle yw - ok - this (method 4) is not the way Delphi calculates the CC - according to my quick test.Oct 10, 2017
- I understand the principles I might have to look at some configurable sub options. Thanks again for the links.Oct 10, 2017
- /subOct 10, 2017
- /interestedOct 10, 2017
- Getting late on the bandwagon, but basically: if there is a branching, you need to add 1. Case statements are glorified IFs, thus for each you need to add 1 and you also need to add 1 for the default "else".
Where it gets really interesting is - however - on the cycles: repeat/until ones should increase it by at least 1 (or more, if you have nested if/case statements) but what about things such as While Not Dataset.Eof do ?
To me, in that case it shouldn't increase because that is part of the normal processing. Obviously, if you have an IF inside, that counts.Feb 26, 2018