Skip to content

Commit 3788b73

Browse files
committed
kill your darlings
1 parent 5ebb692 commit 3788b73

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

_posts/2013-05-19-cyclomatic-and-npath-complexity-explained.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ categories: [complexity]
77
tags: [phpmd]
88
---
99

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.
1111

1212
<!-- more -->
1313

1414
## Cyclomatic complexity
1515

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.
1717

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.
1919

2020
```php
2121
<?php
@@ -66,7 +66,7 @@ The [PHPMD documentation](https://phpmd.org/rules/codesize.html#npathcomplexity)
6666

6767
> The NPath complexity of a method is the number of acyclic execution paths through that method.
6868
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.
7070

7171
> The NPath complexity of a method is the number of acyclic execution paths through that method, that is how many possible outcomes it has.
7272
@@ -94,12 +94,12 @@ foo(11, 20); // Outputs 14
9494
foo(5, 1); // Outputs 23
9595
```
9696

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`.
9898

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.
100100

101-
## So why is the complexity important?
101+
## Why is the complexity important?
102102

103103
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.
104104

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

Comments
 (0)