Is an NPath complexity of over sixteen octillion realistic? Or have I broken the tool?

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

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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật