The Tale Behind Cyclomatic Complexity
The Tale Behind Cyclomatic Complexity
The Tale Behind Cyclomatic Complexity
Introduction
"The cyclomatic complexity measures the complexity of a program and is based on the control flow graph. It is
a count for the number of linearly independent paths through the source code." (From the help of CI).
Ok, the CC goes through a coding block and counts every time, the control flow has a new path at a specific
decision point. A new path will be created due to a "IF", "ELSE", "CASE" and so on.
The CC2 based on the CC and just adds another increment for every path, when a expression uses the
operators "AND" or "OR".
Short: The CC can be described by the following formula:
M=b+1
With 'b' is the number of decision points. A coding block without decision points ( for example just APPEND in
control tables, create of ranges, structures... ) will have at least M = 1.
In CI you can disable this check, but: look at the next topic and you will see, that there is no advantage if you
disable this check.
And now?
With the value of CC, you can measure and ensure the quality and stability of your software. Even the
enhanceability.
1) For each value of CC you should have at least one test to ensure the correctness. E.g. use ABAPUnit and
write so many tests as your CC counts.
2) Have you thought about documentation? If you want to describe what your method do, and you have a CC
of 15 for example, you will need to document at least 15 different states of your method! So, reduce your CC
and you will get an easier software to maintain.
3) When you a have a high CC, you should perform some refactoring with established design-patterns like
factory, command, strategy, chain-of-responsibility ... and you will see, how easy it is to reduce the complexity
by lift up your software to a higher quality degree. For example the CASE can very easy transformed into a
command or strategy pattern. Think about UI-development, where you must handle a lot of events. When you
take the CASE, you have a lot of code to maintain - use a single interface and call it dynamically will reduce
your area to maintain and you can enhance your application very fast and stable.
4) If you have reduced the CC, you will see, that you will have more reusable elements of your software and
you can implement much easier new requirements.
5) Try to think about easier implementations or tools. For example, if you have a very complex business case
with a lot of decisions and operators, you should think about to reduce your complexity by introducing BRF+ as
rules engine.
880 Views Tags: abap
As I start using them more and more - this will really help me develop a better solution. (At least it seems like it
would)
Thank you!
Michelle