Java 什么是 NPath 复杂性以及如何避免它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24670533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is NPath Complexity and how to avoid it?
提问by Wolverine789
In this line:
在这一行:
public Map getAll(BusinessTargetPK pkBusinessTargetId) throws Exception
I am getting this error:
我收到此错误:
NPath Complexity is 32,768 (max allowed is 200)
NPath 复杂度为 32,768(最大允许为 200)
And in this line:
在这一行中:
public Map getAll( Long RLE_ROLE_ID ) throws Exception {
I get this error:
我收到此错误:
The method getAll() has an NPath complexity of 2048
方法 getAll() 的 NPath 复杂度为 2048
I am completely unaware of what is NPath Complexityand what it means.
我完全不知道什么是NPath 复杂性及其含义。
Can someone give advice how to avoid this type of error?
有人可以就如何避免此类错误提出建议吗?
回答by JoeDred
This Link: https://modess.io/npath-complexity-cyclomatic-complexity-explained/
此链接:https: //modess.io/npath-complexity-cyclomatic-complexity-explained/
explains it very well as:
很好地解释了它:
The NPath complexity of a method is the number of acyclic execution paths through that method.
方法的 NPath 复杂度是通过该方法的非循环执行路径的数量。
This means you should avoid long functions with a lot of (nested) if/else statements.
这意味着您应该避免使用大量(嵌套)if/else 语句的长函数。
So my advice would be:
所以我的建议是:
- Split your functions into smaller ones
- Eliminate useless if/else-statements where possible
- 将您的功能拆分成更小的功能
- 尽可能消除无用的 if/else 语句
回答by seeking answers
This is an old thread and Wolverine789 has probably figured out the answer by now but for those who still find this thread in the Google search results, I found the following description of the error by Niklas Modess helpful:
这是一个旧线程,Wolverine789 现在可能已经找到了答案,但是对于那些仍然在 Google 搜索结果中找到该线程的人,我发现 Niklas Modess 对错误的以下描述很有帮助:
https://modess.io/npath-complexity-cyclomatic-complexity-explained/
https://modess.io/npath-complexity-cyclomatic-complexity-explained/