Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Cyclomatic Complexity on Android

How to use this software metric to have more readable and testable code.

 

 
Arildo Borges Jr
Senior Consultant Developer @ ThoughtWorks
Published: March 25, 2021
Tweet
Share
 

Photo by Denys Nevozhai on Unsplash

 

 

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:

 

SonarQube Integration in Android Application (Part -1)

Series Pit Stops

tech.olx.co

 

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

 

Cognitive Complexity™

Cognitive Complexity offers a new measurement of how hard code is to understand — one that strikes developers as…

www.sonarsource.com

 

SOLID Programming (Part 1): Single Responsibility Principle

SOLID principles are among the most valuable in Software Engineering. They allow to write code that is clean, scalable…

developer.android.com

 

Cyclomatic complexity

Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure…

en.wikipedia.org

 

 

 

 

Tags: Android, Solid, Complexity, Development, Mobile

 

View original article at: 


 

Originally published: March 14, 2021

Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code 🗣 With Kotlin Scopes
Noisy Code 🗣 With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
Android News
KMM QuickStart Guide
KMM QuickStart Guide

By Mayank Kharbanda

Kotlin Multiplatform (KMP) is a code-sharing technology that allows us to use the same code for different platforms whether it’s JVM, Android, iOS, watchOS, tvOS, Web, Desktop, or WebAssembly. In this article, we will use Kotlin Multiplatform Mobile (KMM) which is a subset of KMP with the focus on providing better tooling and support for sharing code on mobile platforms i.e. Android and iOS.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One