15

I have just measured a large chunk of PHP code (1153 lines) using PHPMD (http://phpmd.org/) and it tells me the code has an NPath complexity of 16244818757303403077832757824.

That looks like a crazily big number to me, suggesting that perhaps PHPMD has broken in some way. Is it even possible for a piece of code written by humans to have such a high NPath complexity? The cyclomatic complexity is 351.

Two possibly important details -

  1. This was procedural code, mixed in with HTML, and PHPMD will only measure object-oriented code. To get around this, I wrapped the whole file in a class with a single function - this is representative of how it's used.

  2. The file consists of a series of nested switch statements, and inside those there are lots of if..else statements - so it's certainly pretty complicated.

Edit

I want to clarify that I'm not questioning whether PHPMD is lying to me. I know that the code is an awful mess, I just wonder if it's possible for any code to be really that bad. It seems like the answer is yes, it's very possible.

4
  • 2
    I don't know if you broke the tool, but #2 indicates that the code could probably stand to be refactored a bit. May 14, 2015 at 11:37
  • 1
    @LindaJeanne I agree. I'm just curious as to exactly how much of a mess it's in.
    – Jez
    May 14, 2015 at 11:47
  • 2
    WordPress’ WP_Query::get_posts() had a NPath complexity of 1.435 Quindecillion in 2013. It is even worse nowadays …
    – fuxia
    May 15, 2015 at 22:03
  • @toscho that's my new favourite piece of information. Thanks!
    – Jez
    May 16, 2015 at 10:34

2 Answers 2

26

This is entirely possible. Let's assume we have 35 switch-case constructs of 10 cases each, which would give us a rough cyclomatic complexity of 350 when each switch occurs one after the other. The first switch gives us 10 paths. The second switch gives us another independent 10 paths, so that we have 10·10 paths until here. With the third switch, we get 10·10·10=10³ paths, and so on until we get 1035 paths in total! This is even higher than your result of 1.6·1028 paths, which is probably due to a different branching factor, and due to nested control flow statements which reduce the number of paths through your code.

As a worst case scenario for a given cyclomatic complexity c, we can have a maximum of 2c acyclic paths through the code (here: 2351 = 4.6·10105).

The tool's judgement is clear: the code you are dealing with is a convoluted, untestable, and unmaintainable mess. Consider splitting it into smaller, independent functions, and abstracting away repetition. E.g. you could separate HTML generation from the main logic of your PHP script.

2
  • 14
    Thanks for the analysis. I feel the need to point out that it's not my code... but, as is often the case, it does appear to me my problem.
    – Jez
    May 14, 2015 at 11:50
  • 1
    @Jez, if it's any consolation you are not in a unique position. May 20, 2015 at 12:34
5

According to this description, NPath complexity is exponential in cyclomatic complexity.

Taking just simple if statements, if you have two of these statements, that's essentially 4 routes through your code corresponding to the four possible combinations of true/false for the two statement conditions. Add another if statement and you get 8.

In other words, if all your cyclomatic and NPath complexity was coming from a long list of if statements, then your equaltion would be NPath = 2^cyclomatic. Comparing that with your numbers, 2^351 = 4.6 * 10^105, far, far higher than the NPath complexity you reported.

I don't know how much PHPMD does to avoid counting paths which are actually impossible (e.g. two mutually exclusive conditionals both evaluating to true). Possibly a manual analysis would reveal that a lot of the paths are actually impossible, so the code is written in a way that inflates the NPath metric. To continue the above, if you had a list of 351 if statements, but could verify that only one was ever actually entered, you could turn it into a chain of if...else statements, brining your NPath complexity down from 4.6 * 10^105 to 353.

But with only the information in your question, not knowing how much of that kind of simplification could be done or is already being done by PHPMD, the number seems realistic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.