• News

Cyclomatic And NPath Complexity Explained

When software developers use PHP Mess Detector, they may likely encounter two sophisticated terms, namely, cyclomatic complexity and NPath complexity, which they may initially find daunting. However, with cyclomatic and NPath complexity explained in a straightforward way, there’s no need to be overwhelmed by them.

That confident assurance comes from Niklas Modess, a Stockholm-based PHP developer and consultant and the author of the eBook “Deploying PHP Applications” (2020).

PHP Mess Detector (PHPMD), by the way, is PHP Depend’s (a static code analysis program) “spin-off project,” according to the PHPMD website. Its objective is to be like PMD.

PMD, according to the tutorial website Baeldung, is a software tool for different programming languages, such as Apache Velocity, Java, JavaScript, and XSL. It analyzes source code for typical programming errors.

Going back to cyclomatic complexity and NPath complexity, Modess describes them as “fancy words for quite simple concepts.” He also adds that each term “sounds really scary” for some.

Nonetheless, Modess assures that with cyclomatic and NPath complexity explained in the simplest way possible, software developers like him will no longer be intimidated by them.

Explaining Cyclomatic Complexity And NPath Complexity

Sample code showing cyclomatic complexity and NPath complexity
Sample code showing cyclomatic complexity and NPath complexity

Niklas Modess says that cyclomatic and NPath complexity play an important role when writing testable code and maintainable code.

When doing static code analysis, developers use cyclomatic and NPath complexity. These two concepts help developers to assess or gauge the complexity of a certain function.

Cyclomatic Complexity

Cyclomatic complexity is an easy and simple concept, according to Modess, and what it essentially does is to “count some statements.”

Ten points happen to be the normal threshold for cyclomatic complexity. Aim to make it lower if the complexion of the function turns out to be greater than 10.

This will start with 1 point being added for the function declaration. Then what follows next is that for every “case,” “for,” “if,” and “while,” 1 point will be added as well.

A cyclomatic complexity of 12 becomes the result of this function, and to show it, please check the image below.

Example of a function with cyclomatic complexity of 12
Example of a function with cyclomatic complexity of 12

The image above shows an example code taken by Modess from a documentation by PHPMD.

NPath Complexity

When it comes to this one, Modess admits that NPath complexity can be “a bit trickier” than cyclomatic complexity, pointing to a bad PHPMD documentation.

According to PHPMD, as quoted by Modess from its website: “The NPath complexity of a method is the number of acyclic execution paths through that method.”

Upon reading that for the first time, Modess confessed that such an explanation made no sense at all for him. “Zero,” as he bluntly put it. He reiterated what he stressed out earlier: that these are just simple concepts presented using “fancy words.”

For Modess, a simple explanation for NPath complexity is to just say that it’s about “how many ‘paths’ there are in the flow of your code in the function.”

Below is an image that illustrates an example function written by Modess.

Example of a function with NPath complexity of 4
Example of a function with NPath complexity of 4

Based on the image above, 4 is the function NPath complexity. Why? It’s because the function has 4 probable outcomes, as there are 2 statements, and each of these two statements shows 2 probable outcomes (2 * 2 = 4).

There will be a complexity of 8 if one more statement that has 2 probable outcomes gets added (2 * 2 * 2 = 8).

Modess said that NPath complexity could swiftly become unmanageable. One might be surprised to discover functions with complexity greater than 100,000 in legacy code. An NPATH complexity threshold has a default value of 200. To reduce complexity, try to maintain a value lower than 200.

Importance of Complexity

Between simple code and complex code, the former will always be better, according to Modess. When it comes to testability, readability, and maintainability, he said that simple code should be used.

For instance, a unit test needs to be written. The function has an NPath complexity of 18.

What does that one mean?

If you desire a “100% code coverage,” said Modess, then an NPath complexity of 18 means 18 probable outcomes that you need to test. Such could lead to “pretty messy tests,” he warned.

Why is that so?

Modess explained that when writing tests, one naturally intends “as much isolation as possible.” It’s the main reason why, in one’s application, one should strive to constantly lessen the complexity.

Now with cyclomatic and NPath complexity explained, Modess hoped that these two concepts would not anymore overwhelm software developers.

What Is Acceptable Cyclomatic Complexity?

Sample code with acceptable cyclomatic complexity
Sample code with acceptable cyclomatic complexity

In his eponymous blog, business strategist and technologist Brandon Savage describes cyclomatic complexity as “a simple measure of complexity in an application or routine.”

According to him, for the reason that there should always be one code path in any case, cyclomatic complexity therefore should never be lower than 1.

With cyclomatic and NPath complexity explained already by Modess, Savage also offered his views about them.

Savage, who is also the author of the eBooks “Mastering Object Oriented PHP” (2013) and “Practical Design Patterns in PHP” (2014), stated that the acceptable cyclomatic complexity “for most routines,” are the following:

(1) below 4 – “good”

(2) between 5 and 7 – “medium complexity”

(3) between 8 and 10 – “high complexity”

(4) above 10 – “extreme complexity”

As for NPath complexity, in his personal opinion, Savage said that code refactoring is needed for an NPath complexity greater than 140 in order to reduce complexity.

How Can I Improve My Cyclomatic Complexity?

Boston-based software company Axelerant recommends a couple of steps to reduce cyclomatic and the NPath complexity:

(1) “Use small methods.”

(2) “Reduce if/else statements.”

To lower the NPATH value, Checkstyle (a development tool for programmers), in its website, suggests these two methods:

(1) distribute functionality

(2) employ several “if statements as a switch statement”

Conclusion

With cyclomatic and NPath complexity explained in a simple way by a professional expert, one should have now realized where higher numbers will lead them to.

As what Perforce, a Minneapolis-based software company, underscores on its website: “Lower Cyclomatic Complexity = Better Code.”

It’s hard to test a code with a complexity greater than what is acceptable, which also means more chances of making mistakes. Therefore, lower numbers, good; higher numbers, bad.

It can be said that with cyclomatic and NPath complexity explained, the importance of reducing complexity should now be clear.

Recent Articles

  • All About Git Pretty Stats

    All About Git Pretty Stats

    The git status command is used to show what's going on with the repository and the staging area. It lets us see the files that have been tracked and the ones that haven't. This command won't show any commit records or other information, so don't use this command.

  • Laravel Vs Symfony2 - Is There Really A Difference?

    Laravel Vs Symfony2 - Is There Really A Difference?

    The two frameworks share a lot of things. Laravel also uses most of its third-party libraries from Symfony components, so about 30% of Laravel code is written in Symfony. Symfony, on the other hand, is a more complicated framework that is hard to learn, but it is better for big projects because it is more complicated.

  • How To Get Raw POST Data In Laravel

    How To Get Raw POST Data In Laravel

    Laravel's IlluminateHttpRequest class gives you an object-oriented way to interact with the current HTTP request that your application is handling. You can also get the input, cookies, and files that were sent with the request.

  • Cyclomatic And Npath Complexity Explained

    Cyclomatic And Npath Complexity Explained

    Cyclic complexity and NPath complexity are both measured for a method of a function, just like cyclic complexity. It is the number of ways a given piece of code can be run (ignoring cycles).

  • LeetCode – Essential? Optional? Big Deal Or Not?

    LeetCode – Essential? Optional? Big Deal Or Not?

    Are you a developer aiming to put Facebook or Google (or even both!) on your résumé? There are many ways to improve one’s computer skills. LeetCode offers help in that capacity and even promises to get you employed in giant tech companies.

  • The Depths Of AngularJS's Watches

    The Depths Of AngularJS's Watches

    When you want to watch for changes in AngularJS, it is fairly kind to you by default. The kindness comes from trying to optimize performance for you. This is because each watch expressions will run at least once during each $digest loop. I'll not go into when the $digest loop executes and why, and I'll also use watches in controllers in my examples which you should never do, but that's a whole other discussion.

  • Continuous Integration For Laravel With Jenkins And Git

    Continuous Integration For Laravel With Jenkins And Git

    This will be a hands on guide for setting up automated builds for Laravel using Jenkins. Pretty much that when you do a commit, Jenkins will automagically make a build and in that check code errors and syntax, run unit tests and provide visual code coverage for your code base. Along with other goodies. Jenkins is an open source continuous integration server that has gotten a lot of attention recently as the #1 open source continuous integration server. Mostly because its vast amount of plugins, which currently is 600+ as I’m writing this. We’re going to use 10 of those to automate our test environment.