• News

Cyclomatic And NPath Complexity Explained

3.8KShares
127.6KViews

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.

COPYRIGHT_CSWAG: Published on https://codingswag.com/cyclomatic-and-npath-complexity-explained/ by - on 2022-04-13T16:34:18.281Z

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.

Cyclomatic Complexity

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.

Share: Twitter | Facebook | Linkedin

Recent Articles

  • Top Travel Apps To Find You Next Destination

    News

    Top Travel Apps To Find You Next Destination

    How do you choose your travel destination? How do you navigate a new location once you are there? All of these queries are answered by the top travel apps.

  • New Social Media Platforms And Apps That Are Changing The Game

    News

    New Social Media Platforms And Apps That Are Changing The Game

    There are always new social media platforms and apps coming out. Check out this article to learn about new marketing trends and important apps.

  • The Depths Of AngularJS's Watches

    News

    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.

  • Famous Celebrities Who Learn To Code

    News

    Famous Celebrities Who Learn To Code

    Of course, the typical middle or high school kid might not find those careers appealing. However, the truth is that learning to code can set you on a rewarding career path. In fact, a lot famous celebrities who learn to code have mastered coding, and the combined star power of the business may help make it more appealing.

  • Continuous Integration For Laravel With Jenkins And Git

    News

    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.

  • Best Political Campaign Software - How Do Politicians Do Modern Campaigns?

    News

    Best Political Campaign Software - How Do Politicians Do Modern Campaigns?

    The best political campaign software for involving contributors, voters, and supporters is discussed in this article. Additionally, you will learn about the software's capabilities and its main features and pricing options in depth.

  • Best Shopping Apps For Saving Time While Shopping At Home

    News

    Best Shopping Apps For Saving Time While Shopping At Home

    We've compiled a list of the top shopping apps in this article to help you get the best prices, find the ideal goods and services, and save time. If you work as a marketer for companies in these sectors, think about collaborating with these applications to increase brand recognition and customer loyalty.

  • The Top Android NSFW Apps - Unleash Your Sins In Your Phone With The Next Apps

    News

    The Top Android NSFW Apps - Unleash Your Sins In Your Phone With The Next Apps

    No one is the wiser these days because you can stream and download all the HD porn you want for free. You probably came across this page while searching for the top Android NSFW apps because technology has made it simpler than ever to Netflix and unwind.

  • The Top Major Crypto-currency Exchanges In Nigeria - Be Free With Cryptos In Nigeria

    News

    The Top Major Crypto-currency Exchanges In Nigeria - Be Free With Cryptos In Nigeria

    To help you make an informed decision, we'll break with the help of experts from The Nigerian Blogger, an important nigerian website with greart articles and guides for people living in that country (you can read for free $ clicking here$ ) we have made a list with the major crypto-currency exchanges in Nigeria and go over some of the things you should consider.

  • Top Websites To Buy Jewelry Online For Fashion Fans

  • Computer Science Trends - Build An IT Career Studying These Trends

  • Most Interesting Technological Trends - The 2030 Technology

  • All About Git Pretty Stats

  • Coding For Beginners - Easy Way To Learn For Free