I am wondering what Cyclomatic Complexity should have an angular app (each of it's controller, service, etc.) to be easily testable and maintainable. I have found in Microsoft Documentation that Visual Studio has set it's warning level at 25 but I think it's much more language/technology dependant.
2 Answers
Cyclomatic Complexity is not language/technology dependent. It is computed from the logical possible paths in a function.
There's no 'Maximal' cyclomatic complexity. The higher it is, the harder the code is understandable rapidly. The higher it is, the higher the chance that you understand it wrongly and that you introduce a bug in that function if you ever had to do a modification in it.
If your code is to be maintained, or if your code is to evolve, this has to be taken in account. (Maintenance has a cost that is far underestimated.)
This SO question Do you find cyclomatic complexity a useful measure? has some valuable answers.
Personnally I use a rule of 7. (Scientific studies shows that the human brain can grasp about only 7 things/concepts at a time ). I start to question my design when CC is >= 7, when the number of lines of a function is >= 7, when my class has >= 7 data members, when my function has >= 7 parameters... and so on.

- 1
- 1

- 38,876
- 35
- 121
- 169
-
CC > 7 is normally an issue and OP's "25" would be total madness. There is exceptions to that rule though and some apply to some languages only. If the language forces you to use an else-if chain instead of a table look-up, you have the same complexity but formally your CC increased. Also I wonder how you can write an angular controller that deals with a form of more than 5 inputs in less than 7 lines. – Giszmo Mar 13 '15 at 05:05
I guess, there is no definite answer. CC of 25 is most likely not maintainable in any language and yes there are differences from one language to another. In some languages you would be able to throw functions into a dictionary and picking a function from that dictionary would increase CC by one, while in other languages that don't have lambdas you would go with a switch or even an else-if chain that would add one CC per case but maintainability would not be an issue if all the cases end in return;
for example.
As Stephane stated, 7 is a good number and there are static code analysis tools that would default to 7 for Java but you will always run into exceptions where splitting a function into two would formally reduce the CC but humans would have a harder time following what's going on.
Thinking about it, angular controllers would typically have a very low CC (<=2) as they only define functions and variables. These functions though might look "complex" in the sense of CC but promises
adding nothing to CC as the code flow now would be in another function (success()
or failure()
for example).

- 1,944
- 2
- 20
- 47