This content originally appeared on Level Up Coding - Medium and was authored by Ryan NHP
Use Swiftlint and Detekt to Check Cyclomatic Complexity in iOS, Android Projects
What is Cyclomatic Complexity?
Cyclomatic complexity is a measurement developed by Thomas McCabe to determine the stability and level of confidence in a program. It measures the number of linearly-independent paths through a program module.
Its formula is: M = E — N + 2P, where E = number of edges, N = number of nodes and P = number of connected components
Let’s go through with this example to understand how can we calculate this metric.
For example, we have a function that does some logic as the chart below
We have 7 nodes and 8 edges and 1 connected component, so based on the formula we have
M = E — N + 2P = 8-7 + 2 * 1 = 3
What is the limit for Cyclomatic Complexity in one function?
McCabe recommended that the Cyclomatic Complexity for one function should be less than or equal 10.
Besides that, we also have ISO-2626 about Software Metrics that also recommend that the Cyclomatic Complexity should be from 1 to 10.
- Cyclomatic Complexity less than 10: Well written code, High Testability, Cost and Effort is less
- Cyclomatic Complexity from 11 to 20: Complex code, Medium Testability, Cost and Effort is medium
- Cyclomatic Complexity from 21 to 40: Very complex code, Low Testability, Cost and Effort is high
- Cyclomatic Complexity more than 40: Not at all testable, Very high cost and effort
How can we solve these kinds of issues?
Split your high Cyclomatic Complexity functions into some smaller functions. If you have one function that has Cyclomatic Complexity is equal 30, you should split it into at least 3 functions with Cyclomatic Complexity equal or less than 10.
How to check Cyclomatic Complexity for iOS Swift Projects?
Swiftlint is an awesome tool to check your Swift code style and conventions. You can use this to check a lot of rules to make sure your code is good. But I only want to mention about Cyclomatic Complexity.
Install Swiftlint by Homebrew:
brew install swiftlint
Then, create a file with the name is “.swiftlint.yml” in your project directory.
Now, open your terminal and go to a project directory and run swiftlint lint or only simple with swiftlint
This is one of the example errors, I limit Cyclomatic Complexity in my project only 10 but I have this function has 20. That’s smell.
You can also implement Swiftlint to your project and check these rules whenever you build app by
- Go to your project settings, then select Build Phases
- Hit the + button on the top left, next to project app name, then select New Run Script Phase
- Paste this script into that Run Script Phase
After that, whenever you build your app, you will get these warning like this
How to check Cyclomatic Complexity for Android Kotlin Projects?
Detekt will do the same thing as Swiftlint.
Install Detekt by Homebrew:
brew install detekt
Then, must create a file name detekt.yml in your project directory to setup rules.
Set the threshold: 11 to make sure that it’s still good with functions have Cyclomatic Complexity = 10
After that, open terminal and go to your project directory, then run
For a minute, you will get the file name cyclomatic_complexity_report.html in your project directory, open it you will have the report like this
With Swiftlint and Detekt now you can find your code smell and improve it, beside that there’re also some advance rules for this, you should read it on their document pages, For Swiftlint is here, Detetk is here
References
- Cyclomatic complexity - Wikipedia
- GitHub - realm/SwiftLint: A tool to enforce Swift style and conventions.
- detekt
- Mccabe's Cyclomatic Complexity: Calculate with Flow Graph (Example)
Use Swiftlint and Detekt to check Cyclomatic Complexity on iOS and Android was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Ryan NHP
Ryan NHP | Sciencx (2021-09-22T13:08:29+00:00) Use Swiftlint and Detekt to check Cyclomatic Complexity on iOS and Android. Retrieved from https://www.scien.cx/2021/09/22/use-swiftlint-and-detekt-to-check-cyclomatic-complexity-on-ios-and-android/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.