11

There has already been a question on What is Cyclomatic Complexity?

However, there is another term called - Essential Cyclomatic Complexity.

What are the differences and similarities between these two metrics of the code? What are their typical accepted values? Also, I have learned that, for understanding the code, Essential Cyclomatic Complexity is a more relevant metric. Whereas from implementation point of view Cyclomatic Complexity is most relevant. If it is so, why?

Community
  • 1
  • 1
Jay
  • 1,210
  • 9
  • 28
  • 48
  • From a quick glance at wikipedia (presumably you've looked at this already?) ECC appears to be CC after effectively disregarding "well structured" bits of flow control, `if-else`, `for`, `while`, etc. This would then weight control structures that are more difficult to reason about more highly... `break`, `goto`, `throw`, etc. It doesn't reduce the effort required to achieve full test coverage, for example; CC is still relevant in that respect. Was that what you meant, or am I missing something? – Rook Nov 09 '12 at 10:57
  • @Rook Yes, I have been through wiki. I am not concerned about the test coverage here. Lets say, I have been given a code and I have been asked to model (UML diagrams for example) the same by reverse engineering the same, what shall be more relavent to me? ECC or CC? I have read somewhere that ECC is more relavent for understanding the code and CC for implementation. – Jay Nov 09 '12 at 11:01
  • 2
    Personally, I'd say "neither". I'd consider both to be interesting from a project management point of view, when dealing with software development. Outside of that (when modelling an existing system, for example) they seem to be mere curiosities. – Rook Nov 09 '12 at 11:23

1 Answers1

15

Cyclometric Complexity as you know effectively measures the number of possible independent paths through a method or function. This tells us how complex the method is to test.

Essential cyclometric complexity however tells how much complexity is left once we have removed the well-structured complexity. An example of well structured complexity is a for loop where the condition for the loop is stated at the start of the loop. However if we break out of the loop with a break statement for example somewhere in the middle of the path we break our structured component. A similar situation is where we have a number of return statements in a single function.

So what does this tell us ?

Well imagine we have a function with a high CC : a function difficult to test . Now if this function has a low essential CC it means it's fairly easy to break up this function into other smaller functions which are easier to test individually. When essential complexity is high this refactoring is more difficult as the complexity is more difficult to understand.

So code that has a high essential complexity means that he code is harder to maintain and understand. This code we can say is of lower quality. Code that has a high complexity is harder to test but in general we can do someting about this more easily when the essential complexity is low.

What values to use are always up for argument and depend somewhat on the type of application and the language used. For example throwing an exception inside a function makes that function un-structured. Clearly exceptions when used properly are deemed to be a good practise. Similarly validating a parameter at the top of a function and returning immediately is a common practise that (in my opinion) can result in clearer code. Again this is an un-structured construct. So we can imagine that we can accept a basic level of essential complexity.

My personal limits for an enterpise-style application in .NET or Java would be :

CC <= 16 and ECC <= 6

For more 'complex' applications say in C/C++ I would propose tighter limits :

CC <= 10 and ECC <= 4

Tom Carter
  • 2,938
  • 1
  • 27
  • 42
  • I believe code metric results in Visual Studio doesn't provide the details related to essential cyclomatic complexity. Essential cyclomatic complexity can't be inferred from cyclomatic complexity as far as I could understand from your post. What are the ways to get the numbers for essential cyclomatic complexity? – RBT Nov 29 '16 at 12:03
  • @RBT - That's right. The only tool I've ever known to calculate Essential CC is McCabe IQ (http://www.mccabe.com/iq.htm). But its been many years since I've looked – Tom Carter Nov 29 '16 at 23:20