Common Causes of Cyclomatic Complexity and Their Solutions

At work, we consider common problems that lead to high cyclic complexity. For example, having a large if-else statement can lead to high cyclic complexity, but can be resolved by replacing conventions with polymorphism. What other examples did you find?

+4
source share
2 answers

See the definition of Ndepend Cyclomatic Complexity .

Nesting depth is also an excellent code metric.

Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be made in a procedure. Specifically, in C # CC, a method has a value of 1 + {the number of the following expressions found in the method body}:

if | a | for | foreach | case | default | continue | Go to | && | || | catch | ternary operator ?: | ??

The following expressions are not taken into account when calculating CC:

else | to do | switch | try | use | quit | finally | back | creation of objects | method call | access to fields

Adapted to the world of OO, this metric is determined both by methods and classes / structures (as the sum of its SS methods). Note that the CC of an anonymous method is not taken into account when calculating the CC of its external method.

Recommendations Methods where CC is above 15 are difficult to understand and maintain. Methods in which CC is above 30 are extremely complex and must be separated by smaller methods (unless they are automatically generated by the tool).

+5
source

Another example to avoid using so many if's is the implementation of a state machine. Since events overtake transitions, therefore conditional expressions are clearer with these transitions, which change the state of the System. Management is simpler.

Leave a link that mentions some of its benefits:

http://www.skorks.com/2011/09/why-developers-never-use-state-machines/

+1
source

Source: https://habr.com/ru/post/1300081/


All Articles