McCabe Cycles In Flutter

One of the challenges of designing packages or libraries is in how does one tell when you need to break up the api into more modules. One could compute the McCabe cycles by-hand but than you would have to remember all that math.
I am going to show you how to use a dart plugin to compute the McCabe metric so that you can see when you reach the McCabe 10 cycle metric and know that you should break up your api into more modules.
The Dart Code Metrics Plugin
The Dart Code Metrics is by the Wrike TechClub Open Source Community(https://github.com/wrike). Let me show you how to install this plugin.
The first step to install this plugin is to add the dependency to your pubspec:
dev_dependencies:
dart_code_metrics: ^2.4.0
The second step is to than put your metrics configuration in the analysis_options.yaml file. See my Lint Like A Boss(https://fredgrott.medium.com/lint-like-a-boss-60b85e82c227) for why you should have an analysis_options.yaml lint file in your flutter project. The analysis_options configuration block:
analyzer:
plugins:
- dart_code_metricsdart_code_metrics:
anti-patterns:
- long-method
- long-parameter-list
metrics:
cyclomatic-complexity: 20
lines-of-executable-code: 50
number-of-arguments: 4
maximum-nesting: 5rules:
- newline-before-return
- no-boolean-literal-compare
- no-empty-block
- prefer-trailing-comma
- prefer-conditional-expressions
- no-equal-then-else
Note, I do not exclude the unit test folder as since we are going for full test coverage here we should also be computing McCabe cycle complexity as far as our unit test code coverage. By including the test subfolder code you wil be able to see in the metric reports if you included enough unit tests to fully test the app code.
Now for the final step. Since we are using this from a Flutter Sdk with the Dart Sdk in the Flutter Sdk the commands have a flutter prefix, to activate:
flutter pub global activate dart_code_metrics
And to get an html report rather than console when the analyzer runs the command-line instructions will be:
flutter pub global run dart_code_metrics:metrics lib --reporter=html
that will produce an html report in a sub-folder named metrics. As you can see you get a summary report:

and you get a detailed report for every class:

Now for some McCabe cyclomatic complexity background.
McCabe Cycle Background
It’s complexity M:
M = E − N + 2P
where
E = the number of edges of the graph.
N = the number of nodes of the graph.
P = the number of connected components.
it’s not required that you know that it’s based on algebraic topography, remember it’s the number of control paths and that we want less than 20.
Conclusion
By using the Dart Code Metrics plugin to automate computing the cyclomatic complexity of both your app code and test code you can not only better architect your applications api and also get better test code coverage in knowing what to unit test.
It takes all the math pain out of the actual McCabe cyclomatic calculation in a no mess and no fuss way. And plus, why not have an actual measure of test coverage beyond an empty 100% code coverage?
Trademark Notice
Google LLC owns the following trademarks; Dart, Flutter, Android, Roboto, Noto. Apple Inc owns the trademarks iOS, MacOSX, Swift, and ObjectiveC. Apple Inc owns trademarks to their fonts of SF Pro, Sf Compact, SF mono, and New York. JetBeans Inc owns the trademarks to JetBeans, IntelliJ, and Kotlin. Oracle Inc owns the Java trademark. Microsoft Inc owns the trademarks to MS Windows OS and Powershell. Gradle is a trademark of Gradle Inc. The Git Project owns the trademark to Git. Linux Foundation owns the trademark to Linux. SmartPhone OME’s own trademarks to their mobile phone product names. To the best of my ability I follow the brand and usage guidelines with the above mentioned trademarks.
Resources
Specific article resources:
Cyclomatic Complexity@wikipedia https://en.wikipedia.org/wiki/Cyclomatic_complexity
Dart Code Metrics@pub.dev https://pub.dev/packages/dart_code_metrics
General Flutter Resources:
Flutter Community Resources https://flutter.dev/community
Flutter SDK https://flutter.dev/docs/get-started/install
Android Studio IDE https://developer.android.com/studio
MS’s Visual Studio Code https://code.visualstudio.com/
Flutter Docs https://flutter.dev/docs
Dart Docs https://dart.dev/guides
Google Firebase Mobile Device TestLab https://firebase.google.com/docs/test-lab
About Fred Grott
I’m the crazy SOB who as a former android mobile developer is starting to write about flutter mobile app development, design, and life(see Eff COVID and GOP https://fredgrott.medium.com/eff-covid-and-the-gop-e912db0548b8). Will I reach the pivotal One Million Medium monthly viewers mark? Sit-back and watch it happen. Find me on these social platforms: