• News

Cyclomatic And NPath Complexity Explained

7.8KShares
261.7KViews

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

  • Meet The Richest Tech Billionaires In 2023

    News

    Meet The Richest Tech Billionaires In 2023

    During the stock market's sharp drop, the richest tech billionaires lost a total of close to $500 billion. This is more than the market value of all but seven of the S&P 500 companies.

  • Political Opinion Analysis - Understanding Public Sentiment Using Programming Languages

    News

    Political Opinion Analysis - Understanding Public Sentiment Using Programming Languages

    Discover the Power of Political Opinion Analysis with Programming Languages: Learn how Python and R can help shape public discourse, inform policy decisions, and forecast election outcomes. Explore the applications and limitations of political opinion analysis, and delve into the ethical considerations that researchers need to keep in mind. Unlock the potential of political opinion analysis and stay ahead in the world of politics with programming languages.

  • What Is A Dogecoin Faucet?

    News

    What Is A Dogecoin Faucet?

    Dogecoin can be obtained for free using a "doge faucet" that distributes small quantities of doge in exchange for the user performing some action. You are, in essence, trading your time and attention in exchange for the opportunity to earn free Dogecoin.

  • The Importance Of Web Development In Digital Marketing

    News

    The Importance Of Web Development In Digital Marketing

    This article explains the web development process and the role of web design in creating an effective website optimized for SEO and user experience. It also discusses the essential elements of e-commerce web development and how to choose the right web development framework.

  • The Role Of Science And Technology In Our Daily Life

    News

    The Role Of Science And Technology In Our Daily Life

    Due to the role that they play in making life easier, faster, and safer, science and technology are essential to our daily lives. Let's examine the role that science and technology play in the modern world.

  • Technology Is Changing News - An Insight View About The Future Of Media

    News

    Technology Is Changing News - An Insight View About The Future Of Media

    The news industry is evolving quickly and technology is changing news. Laptops and pocket PCs have displaced desktops. Numerous brand-new material sources have been developed and connected via the internet.

  • The Role Of Programming In Architecture

    News

    The Role Of Programming In Architecture

    This article explores the role of programming in architecture, highlighting its impact on collaboration, sustainability, personalized spaces, cost efficiency, and innovative design. Discover how programming is revolutionizing the field of architecture and shaping the future of the industry.

  • Kenya Technology - A Hidden Industry

    News

    Kenya Technology - A Hidden Industry

    The northeastern African country of Kenya is a tiny coastal one. Kenya is praised as a top tourist destination for its tea exports, stunning scenery, and abundant biodiversity. Currently, Kenya technology industry is growing quickly.

  • Best Streaming Services For 2023 That You Should Check Out

    News

    Best Streaming Services For 2023 That You Should Check Out

    Our best recommendations for both on-demand and live streaming services are included in our ranking of the best streaming services for 2023. We've evaluated market leaders like Netflix and Hulu as well as services with a more specialized focus, such as anime streaming portal Crunchyroll and sports-focused fuboTV.

  • Top Travel Apps To Find You Next Destination

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

  • The Depths Of AngularJS's Watches

  • Famous Celebrities Who Learn To Code

  • Continuous Integration For Laravel With Jenkins And Git