I've recently come across Cyclomatic Complexity and I'd like to try to understand it better.
What are some practical coding examples of the different factors that go into calculating the complexity? Specifically, for the Wikipedia equation of
M = E − N + 2P, I want to better understand what each of the following terms means:
- E = the number of edges of the graph
- N = the number of nodes of the graph
- P = the number of connected components
I suspect that either E or N may be the number of decision points (if, else if, for, foreach, etc) in a block of code, but I'm not quite sure which is which or what the other signifies. I'm also guessing that P refers to function calls and class instantiations, but there isn't a clear definition given that I can see. If someone could shed a little more light with some clear code examples of each, it would help.
As a follow-up, does the Cyclomatic Complexity directly correlate to the number of unit tests needed for 100% path coverage? As an example, does a method with a complexity of 4 indicate that 4 unit tests are needed to cover that method?
Finally, do regular expressions affect Cyclomatic Complexity, and if so, how?