Sunday, February 28, 2021

What Is Cyclomatic Complexity? Measuring Code Quality

- Advertisement -
- Advertisement -
- Advertisement -
Shutterstock/fran_kie

Cyclomatic Complexity is a code metric that you can view in many IDEs like Visual Studio. While it’s not an exact science, it allows you to get a general idea of the complexity of functions, classes, and namespaces, which can be useful when looking for code to refactor.

What Is Cyclomatic Complexity?

Cyclomatic complexity basically measures how much your code branches. Every time there’s an if statement or other control block like a loop, cyclomatic complexity goes up, since the graph will look more and more like a tree.

If you imagine your code as a series of actions (functions, method calls, variable assignments) connected via control flow, you’ll get an abstract graph that you can use to better understand the complexity. For common control flows like if statements and for loops, the graphs look like this:

The formula for it is simple; take the number of edges in the graph (the arrows connecting everything) and subtract the number of nodes in the graph (the actions themselves).

Complexity = Edges -- Nodes + 2

For example, this code has a cyclomatic complexity of one, since there aren’t any branches, and it just calls WriteLine over and over. Since it’s just perfectly linear code, the number of nodes will cancel out the number of edges, giving a cyclomatic complexity of one.

public void TestFunction()
{
    Console.WriteLine("Test");
    Console.WriteLine("Test");
    Console.WriteLine("Test");
    Console.WriteLine("Test");
    Console.WriteLine("Test");
}

For more complicated code with branches, the complexity will be higher. This code, which contains a switch statement, has a complexity of 6, because there are many different paths the code can take. Each case in the switch statement adds complexity, as it can lead to different outputs with differing inputs.

public void TestFunction(string arg)
{
    switch(arg)
    {
        case "one":
            Console.WriteLine("Test");
            break;
        case "two":
            Console.WriteLine("Test");
            break;
        case "three":
            Console.WriteLine("Test");
            break;
        case "four":
            Console.WriteLine("Test");
            break;
        case "five":
            Console.WriteLine("Test");
            break;
    }
 
    Console.WriteLine("Test");
}

Cyclomatic complexity is only calculated within the scope of the function. If a function calls another function that has a high cyclomatic complexity, it’s only counted as a single node, and doesn’t add anything to the caller, despite technically adding complexity to the program in a general sense.

Is Cyclomatic Complexity Useful?

Cyclomatic complexity isn’t a perfect metric. It’s a very basic metric and looks over the nuance of the code itself. Of course, you can still have terrible code with low complexity, or decent code with high complexity. But, in general, it’s still quite useful for getting a roundabout sense of how complex a program is.

For the most part, complexity under 6 to 8 is probably fine as long as the code itself is well formatted. Anything from 8-15 is questionable, and anything over 15 is probably not great. Anything over 25 is almost certainly a problem unless proven otherwise.

While having a high cyclomatic complexity in any given function isn’t the end of the world, it can be indicative of a larger problem. High complexity functions are harder to maintain and prone to more bugs, since there are more things to go wrong. And higher complexity functions directly lead to higher complexity unit tests, which can make the code hard to maintain in the long run due to the difficulty of testing.

Visual Studio and other IDEs will calculate aggregate complexities of entire classes and namespaces, which can be useful for tracking down your most complex classes. You can sort by highest complexity and drill down into individual functions.

Often, code review can take cyclomatic complexity into account, even flagging problematic functions that may need manual review. This can make it a very useful tool for maintaining a clean and orderly codebase.

Looking For Bad Code

Many IDEs, like Visual Studio, will have built in tools for calculating cyclomatic complexity and other code metrics for your entire codebase.

To do so from Visual Studio, click Analyze > Calculate Code Metrics > For Solution.

This will bring up the “Code Metrics” panel that will show a breakdown for your solution. You can sort by complexity in descending order to view the most problematic namespaces.

Alongside complexity, Visual Studio also presents a “Maintainability Index” which scores the method from 0-100 on high easily it can be maintained, as well as “Class Coupling,” which lists how many classes are referenced from that function or class.

Here, Visual Studio pointed out a 400 line method of mine that scored a whopping 72 on the complexity scale as well as a 14/100 on the maintainability index (presented as a yellow triangle danger sign), and references 65 different classes.

You may be sent directly into the five stages of grief at the result. “But, this is a really long coroutine that has a bunch of tasks to do!” I tell myself, while trying to deny that the code I wrote is mathematically bad, to the point where Visual Studio throws a warning. A complexity of 72 is certainly in need of cleaning up.

In this case, the fix was simple — the coroutine has a bunch of tasks to do, so I break those tasks up into smaller coroutines, and replace the main method with calls to subroutines. The overall code didn’t change, and neither did the total complexity of the class itself, but now the main function isn’t a 400 line monstrosity.

Read more from a Random Article pick

TRENDING NOW

Pedophile Boy Scout Leader Forced Children to Gang-Rape Girl, 7, Made them swear on scout code of honor to keep quiet

The horrific event had taken place in Germany at a scout lodge basement and the leader used the ‘scout code of honor’ with the kids making them swear that they would never talk about what had happened, according to The Sun.

This 27-course bundle can help you learn to code this new year for just $60

TLDR: With 27 courses and over 270 hours of coursework, The Premium Learn to Code 2021 Certification Bundle is the one-stop shop for becoming a well-trained web developer.

Zoho Creator is how to create basic business apps even if you don’t know how to code

With a one-year subscription to Zoho Creator, you can build dozens of business apps for improving your workflows.

YOU MAY ALSO LIKE

Zoho Creator is how to create basic business apps even if you don’t know how to code

With a one-year subscription to Zoho Creator, you can build dozens of business apps for improving your workflows.

Pedophile Boy Scout Leader Forced Children to Gang-Rape Girl, 7, Made them swear on scout code of honor to keep quiet

The horrific event had taken place in Germany at a scout lodge basement and the leader used the ‘scout code of honor’ with the kids making them swear that they would never talk about what had happened, according to The Sun.

This 27-course bundle can help you learn to code this new year for just $60

TLDR: With 27 courses and over 270 hours of coursework, The Premium Learn to Code 2021 Certification Bundle is the one-stop shop for becoming a well-trained web developer.

Stay Connected

40,345FansLike
2,394FollowersFollow
870FollowersFollow
- Advertisement -

Latest Articles

The time Will Smith punched Anthony Mackie in the face

"I'm on stage with Will Smith doing a concert," Mackie recalled (in our imagination, the Fresh Prince was performing "I Think I Can Beat Mike Tyson").

The 7 best and 7 worst horror movies of 2020

Impetigore, a Indonesian horror film from writer-director Joko Anwar, follows Maya (Tara Basro), who has inherited a house in her ancestral village.

Cry baby! Son of Supreme Court Judge who stormed the Capitol armed with a stick weeps in court as he is facing federal charges

The 34-year-old stormer is the son of the prominent Kings County Supreme Court Judge Steven Mostofsky (Shlomo).

Why you should probably stop pre-ordering video games

Pre-ordering a game means that you're guaranteed to pay full price for it, no matter how much you enjoy it.

What young Jenny from Forrest Gump looks like now

Not a year after Forrest Gump, Hall was already onto her next acting job with a film adaptation of Goldilocks and the Three Bears, followed up by several TV films.
- Advertisement -
Storepaperoomates Blog

Pin It on Pinterest