Cyclomatic Complexity on Android
How to use this software metric to have more readable and testable code.

Cyclomatic Complexity
First, what is Cyclomatic Complexity?
Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program’s source code. Its formula is: M = E - N + P, where E = number of edges, N = number of nodes and P = number of connected components
Based on this definition, it is possible to conclude that cyclomatic complexity is directly related to the maximum number of paths that the source code can execute, such as an if conditional:
The above code can be represented as follows:

flowchart represented above code
According to the formula for calculating cyclomatic complexity, we have:
M = 5 (edges) — 5 (nodes) + 2 (connected components) = 2
So the complexity of the sample code is 2, because that is the maximum number of paths that the code can perform.
Importance of this metric on Android
As previously seen, this metric is directly linked to the logical complexity of the code, but this also reflects in the readability of the code, as as the number of possible paths increases, so does the difficulty of reading this code, becoming increasingly difficult to maintain. Another good point is the difficulty of writing unit tests, since it will be necessary to cover all Npossibilities within the same function.
How to generate this metric on Android
There are several ways and tools to extract cyclomatic complexity on Android, but I recommend the SonarQube tool, which in addition to offering several other metrics, still supports several other languages, not being restricted only to android:
How to decrease the complexity of a code
A good strategy is to extract specific codes for specific functions so that, in addition to making the code more readable, it also reduces the number of paths that a single function has, as they will be diluted in other functions. This approach is very much in line with what the SRP (Single Responsibility Principle, the “S” in SOLID) defines:
However, according to the Single Responsibility Principle, a module, a class or a function has to only do one thing. In other words, they have to have only one responsibility. This way the code is more robust, easier to debug, read and reuse.
See this approach in practice, using very simple code:
as there are 5 possible paths in the code above, its complexity is 5
The code above has a complexity of 5. One way to decrease this number and at the same time conform to the SRP would be to extract the `when` code to a separate function, as its responsibility is not to display the toast, but to define whether it should be displayed or not, according to the type of user:
Note that in the code above, the functionality has not been changed, but now it has a complexity of 2 (1 component + 1 conditional if). This allows for a greater granularity of tests, since it will be possible to test two functions separately, showUserNotification()
and shouldShowAdsToUser()
, each with its respective responsibility.
As much as this is a simple example, you can already see the gains from this approach, now imagine the difference in the real world, with large functions, legacy code, etc …
Conclusion
Although analyzing the cyclomatic complexity of a code is a very important point, it alone does not guarantee that you have a very readable code:
source: https://www.sonarsource.com/resources/white-papers/cognitive-complexity.html
source: https://www.sonarsource.com/resources/white-papers/cognitive-complexity.html
See the two examples above, both have the same complexity, 4 but totally different legibility.
The ideal is to always use it in conjunction with other metrics and invest in good development practices, especially SOLID principles, which are a great reference for you to have a readable, testable and easy to maintain code.
Tip: I recommend reading about a second metric related to this subject, Cognitive Complexity, which is directly linked to the readability and readability of the code.
Thanks!
References and useful links
Tags: Android, Solid, Complexity, Development, Mobile
View original article at:

Originally published: March 14, 2021