<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=278116885016877&amp;ev=PageView&amp;noscript=1">

, ,

Jul 26, 2019 | 2 Minute Read

Reducing Cyclomatic Complexity and NPath Complexity: Steps for Refactoring

Table of Contents

Introduction

Have you been in situations where time constraints force you to spend less time on code optimization? We all have! We want to spend more time on writing the actual logic than to check if we are following best practices in the code. The whole point of creating coding standards is to reduce the burden of figuring out best practices individually on each developer. On the bright side, once specific coding standards are set, we can create programs to check if our code meets them. And if not, this program can also let us know what can be done about it.

Different ways of measuring complexity of source code are available. The most commonly used measures are:

  • Cyclomatic complexity
  • NPath complexity
     

Let’s focus on these two code complexity measures today.

Cyclomatic Complexity

This complexity is calculated for a method or a function. The cyclomatic complexity of a method without any branching code, such as ‘if’, ‘for’, ‘while’, ‘case’, ‘switch’, ‘try-catch’ statements, in the method is one. To calculate the cyclomatic complexity of a method, a decision point is assigned for every statement which branches code; think of a rhombus in an algorithm flowchart. Each decision point increases the cyclomatic complexity of the method by one.

PHPMD uses 10 as the threshold for cyclomatic complexity.

The code below is the example given in the documentation. Notice the numbers to the left of the lines. Give one for the function declaration and add one for each decision point. The final number is the cyclomatic complexity of that method.

NPath Complexity

Similar to cyclomatic complexity, NPath complexity is also calculated for a method of a function. NPath complexity is the number of execution paths possible for a given code i.e. the number of ways the given code can get executed (ignoring cycles).

Consider the code below. There are two if/else statements, each giving two path options. So, the code can be executed in 4 (2 * 2) times, which means the NPath complexity of the code is four.

Reducing Cyclomatic Complexity

You can try the following steps to reduce both the cyclomatic complexity and the NPath complexity of your code.

  1. Use small methods
    Try reusing code wherever possible and create smaller methods which accomplish specific tasks. This can significantly reduce the number of lines and improve readability of your code.
  2. Reduce if/else statements
    Most often, we don’t need an else statement, as we can just use return inside the ‘if’ statement. Consider the code below.

“Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write.”

― Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship

Coding standards are essential for ensuring good readability and maintainability of your code. Reducing cyclomatic complexity can make writing test cases easier. There are many tools available to help you write good code. If you are a Drupal or PHP developer, check out this link for more information on various tools you can use.

About the Author
Shashikanth Reddy, Axelerant Alumni
About the Author

Shashikanth Reddy, Axelerant Alumni


Back to Top