NPath复杂性

Wol*_*789 8 java pmd sonarqube

在这一行:

public Map getAll(BusinessTargetPK pkBusinessTargetId) throws Exception
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

NPath复杂度为32,768(最大允许值为200)

在这一行:

public Map getAll( Long  RLE_ROLE_ID  ) throws Exception {
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

getAll()方法的NPath复杂度为2048

我完全不知道什么是NPath复杂性及其含义.

有人可以提供建议如何避免这种类型的错误?

Joe*_*red 14

此链接:https://modess.io/npath-complexity-cyclomatic-complexity-explained/

解释得非常好:

方法的NPath复杂性是通过该方法的非循环执行路径的数量.

这意味着你应该避免使用很多(嵌套的)if/else语句的长函数.

所以我的建议是:

  1. 将您的功能拆分为较小的功能
  2. 尽可能消除无用的if/else语句

  • 方法的NPath复杂性是通过该方法的非循环执行路径的数量或简单的解释是在函数的代码流中有多少"路径". (4认同)

小智 8

这是一个旧线程,Wolverine789 现在可能已经找到了答案,但是对于那些仍然在 Google 搜索结果中找到此线程的人,我发现 Niklas Modess 对错误的以下描述很有帮助:

https://modess.io/npath-complexity-cyclomatic-complexity-explained/

  • 链接可能变得不可用。请描述链接中写的内容 (2认同)