If you’re interested in learning more about Helix QAC and how it can help with cyclomatic complexity, sign up for our next live demo.
Today, I’m going to discuss:
- What is cyclomatic complexity;
- What are the effects of high cyclomatic complexity on your code;
- How to calculate cyclomatic complexity; and
- How cyclomatic complexity can be effectively managed with Helix QAC.
Let’s start by defining what cyclomatic complexity is.
Cyclomatic complexity is a metric for software quality developed by Thomas J. McCabe Sr., and refers to the number of linearly independent paths through a program’s source code.
The higher the number, the more complex the code. And, the more complex the code, the more likely it will:
- Have defects;
- Be difficult to test;
- Be difficult to read; and
- Be difficult to maintain.
Simply put, complex code is unreliable, inefficient, and of low quality. That is why measuring cyclomatic complexity is important, so you can lower it — if necessary — and improve the quality.
Now, let’s move on to how you can calculate cyclomatic complexity.
To calculate cyclomatic complexity, use this formula:
E – N + 2P
- E refers to the number of edges or transfers of control;
- N refers to the number of nodes or sequential group of statements containing only one transfer of control; and
- P refers to the number of disconnected parts of the flow graph, such as a calling program and a subroutine.
The equation represents the number of decisions plus one.
In addition, binary decisions — such as “if” and “while” statements — add one to complexity. While Boolean operators can add either one or nothing to the complexity. For example, one may be added if a Boolean operator is found within a conditional statement.
Code with a cyclomatic complexity between 1 and 10 is considered to be clean, testable, effective, and manageable code. Beyond 10, the risk of defects greatly increases.
To help you better understand what cyclomatic complexity is, here is an example:
Take a moment to look over the code. Okay. Here is what you should have come up with: As there is a Boolean operator, there are two decisions making the cyclomatic complexity is 3.
There are variations of cyclomatic complexity that you should be aware of as well, such as the Myers’ Interval.
The Myers’ Interval is an extension of cyclomatic complexity and accounts for the complexity caused by compound predicates. It uses cyclomatic complexity as its lower bound. The upper bound is defined as the total number of conditions in the code plus one. It’s presented as two values separated by: cyclomatic complexity: “number of logical operators”.
To help you better understand how to use the Myers’ Interval, here’s an example:
Take a moment to review the code. Okay. Here is what you should have come up with: It’s cyclomatic complexity is 3, and the there is one connective used in the conditions. So, its Myers’ Interval cyclomatic complexity is 3:4.
Manual review of cyclomatic complexity can be a time-consuming process that is vulnerable to human error. However, with a static analyzer tool — such as Helix QAC — the process is quick, efficient, error free.
Helix QAC makes it easy for you to track and monitor the cyclomatic complexity of your codebase over the entire life cycle of your project.
To give you a clear understanding of how the tool works, let’s move over to the QAC dashboard. From the dashboard, we can view different snapshots of our code analysis results.
We’ll start by taking a look at the Metric Trend Graph. This graph shows how much work is required to bring the cyclomatic complexity down to an acceptable value.
Let’s move on to the final tab and take a look at our custom reports. [Go to pre-opened tab]
Starting with the metric trend table, we can configure this so we can view how our functions have been trending overall snapshots. Or, we can limit this to just the most recent.
If we look at the metric threshold table, we can see the minimum and maximum thresholds for each given metric. And, which ones are exceeding the limits.
Moving on to the function structure diagram, we have a graphical representation of all the decision points in each function. This can help reduce your function’s complexity once you’ve identified where there appears to be duplicated code. And, once that has been refactored, it will make your code a lot easier to maintain.
Finally, let’s view the Helix QAC desktop GUI to see our custom messages where the complexity has exceeded 10.
These diagnostics appear in green to stand apart from the standard Helix QAC diagnostics and make it quick and easier to identify your custom messages.
Managing cyclomatic complexity is just one example of how Helix QAC makes it easy to effectively manage your codebase.