You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2013-05-19-cyclomatic-and-npath-complexity-explained.md
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -7,15 +7,15 @@ categories: [complexity]
7
7
tags: [phpmd]
8
8
---
9
9
10
-
If you happen to be using [PHP Mess Detector](https://phpmd.org/) (which you should for any larger project) you have probably stumbled upon these two, but do you really know what they stand for? NPath complexity and cyclomatic complexity sounds really scary, but they are fancy words for quite simple concepts. So let's go through them and also find out why they're important for maintainable and testable code. Both of these concepts are used in static code analysis and are measurements of how complex a function is.
10
+
If you happen to be using [PHP Mess Detector](https://phpmd.org/) (which you should for any larger project) you have probably stumbled upon these two, but do you really know what they stand for? NPath complexity and cyclomatic complexity sounds really scary, but they are fancy words for quite simple concepts. Let's go through them and find out why they're important for maintainable and testable code. Both of these concepts are used in static code analysis and are measurements of how complex a function is.
11
11
12
12
<!-- more -->
13
13
14
14
## Cyclomatic complexity
15
15
16
-
This is a very straight forward concept, it's pretty well [documented](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) in PHPMD's documentation and what it does is pretty much count some statements. The standard threshold for this complexity is 10 points, so if you have a function with higher complexity than that, you should try to reduce it.
16
+
This is a very straight forward concept, it's pretty well [documented](https://phpmd.org/rules/codesize.html#cyclomaticcomplexity) in PHPMD's documentation and what it does is pretty much count some statements. The standard threshold for this complexity is 10 points, if you have a function with higher complexity than that, you should try to reduce it.
17
17
18
-
It will begin by adding 1 point for the function declaration, after that it will add 1 point for every `if`, `while`, `for` and `case`. I'll just copy and paste the example code from PHPMD's documentation to illustrate this, of how this function ends up with a cyclomatic complexity of 12.
18
+
It will begin by adding 1 point for the function declaration, after that it will add 1 point for every `if`, `while`, `for` and `case`. I'll copy the example code from PHPMD's documentation to illustrate how this function ends up with a cyclomatic complexity of 12.
19
19
20
20
```php
21
21
<?php
@@ -66,7 +66,7 @@ The [PHPMD documentation](https://phpmd.org/rules/codesize.html#npathcomplexity)
66
66
67
67
> The NPath complexity of a method is the number of acyclic execution paths through that method.
68
68
69
-
Huh, what? That made no sense to me. Since initially writing this blog post, I finally contributed [a pull request](https://github.com/phpmd/phpmd/pull/954) to make more sense of it. As previously stated, it's fancy talk for simple stuff. The simple explanation is how many "paths" there are in the flow of your code. Or it could be described as the number of possible outcomes for a function/method. Hence my suggested update to the documentation, with the following example code included.
69
+
Huh, what? That made no sense to me. Since initially writing this blog post, I finally contributed [a pull request](https://github.com/phpmd/phpmd/pull/954) to make more sense of it. It's fancy talk for simple stuff. The simple explanation is how many "paths" there are in the flow of your code. Or it could be described as the number of possible outcomes for a function/method. Hence my suggested update to the documentation, with the following example code included.
70
70
71
71
> The NPath complexity of a method is the number of acyclic execution paths through that method, that is how many possible outcomes it has.
72
72
@@ -94,12 +94,12 @@ foo(11, 20); // Outputs 14
94
94
foo(5, 1); // Outputs 23
95
95
```
96
96
97
-
So here we have a function with 4 possible outcomes, since we have 2 statements that have 2 possible outcomes each (`2 * 2 = 4`). That means that the function's NPath complexity is 4. If we would add another statement with 2 possible outcomes we would get a complexity of 8 since `2 * 2 * 2 = 8`.
97
+
Here we have a function with 4 possible outcomes, since we have 2 statements that have 2 possible outcomes each (`2 * 2 = 4`). That means that the function's NPath complexity is 4. If we would add another statement with 2 possible outcomes we would get a complexity of 8 since `2 * 2 * 2 = 8`.
98
98
99
-
So the NPath complexity is exponential and could easily get out of hand, in old legacy code don't be surprised if you find functions with complexity over 100 000. The default value of the threshold for this complexity is 200, staying under that value is something you should strive for.
99
+
The NPath complexity is exponential and could easily get out of hand, in old legacy code don't be surprised if you find functions with complexity over 100 000. The default value of the threshold for this complexity is 200, staying under that value is something you should strive for.
100
100
101
-
## So why is the complexity important?
101
+
## Why is the complexity important?
102
102
103
103
Simple code is always better than complex code. This applies to areas as readability, maintainability and testability. Consider writing a unit test and you have a function with an NPath complexity of 16. This means that if you want 100% code coverage you need to test for 16 possible outcomes and that would end up in pretty messy tests. This goes against the entire philosophy of unit testing since you want as much isolation as possible when writing your tests. That's why you should always try to reduce complexity in your application.
104
104
105
-
So don't be scared of these seemingly scary concepts but instead embrace them and use them to your advantage.
105
+
Don't be scared of these seemingly scary concepts but instead embrace them and use them to your advantage.
0 commit comments