java 超越圈复杂度的代码复杂度分析工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1143084/
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
Code complexity analysis tools beyond cyclomatic complexity
提问by Jim Rush
While cyclomatic complexity is a worthwhile metric, I tend to find it to be a poor tool for identifying difficult to maintain code. In particular, I tend to find it just highlights certain types of code (e.g. parsers) and misses difficult recursion, threading and coupling problems as well as many of the anti-patterns that have been defined.
虽然圈复杂度是一个有价值的指标,但我倾向于发现它不是识别难以维护代码的糟糕工具。特别是,我倾向于发现它只是突出了某些类型的代码(例如解析器),而忽略了困难的递归、线程和耦合问题以及许多已定义的反模式。
What other tools are available to identify problematic Java code ?
还有哪些其他工具可用于识别有问题的 Java 代码?
Note, we already use PMD and FindBugs which I believe are great for method level problem identification.
请注意,我们已经使用了 PMD 和 FindBugs,我认为它们非常适合方法级问题识别。
采纳答案by Jim Rush
My experience is that the most important metrics when looking at code maintainability are:
我的经验是,在查看代码可维护性时,最重要的指标是:
- Cyclomatic Complexity, to identify large chunks of code that are probably hard to understand/modify.
- Nesting depth, to find similar spots (a high nesting depth is automatically high CC, but not necessarily the other way around, so scoring on both is important to look at).
- Fan in/out, to get a better view of the relationships between methods/classes and the actual importance of individual methods.
- 圈复杂度,用于识别可能难以理解/修改的大块代码。
- 嵌套深度,以找到相似的点(高嵌套深度自动是高 CC,但不一定相反,因此对两者进行评分很重要)。
- 扇入/扇出,以更好地了解方法/类之间的关系以及各个方法的实际重要性。
When examining code that was written by others, it is often useful to include dynamic techniques. Simply run common usage scenarios through a profiler/code coverage tool to discover:
在检查其他人编写的代码时,包含动态技术通常很有用。只需通过分析器/代码覆盖工具运行常见的使用场景即可发现:
- Code that is actually executed a lot (the profiler is great for this, just ignore the timing info and look at the hit counts instead).
- Code coverage is great to find (almost) dead code. To prevent you from investing time in refactoring code that is rarely executed anyway.
- 实际执行了很多的代码(分析器对此非常有用,只需忽略计时信息并查看命中计数即可)。
- 代码覆盖率非常适合查找(几乎)死代码。防止您在重构很少执行的代码上投入时间。
The usual suspects such as any profiler, code coverage and metrics tool will usually help you with getting the data required to make these assessments.
任何分析器、代码覆盖率和度量工具等通常的嫌疑人通常会帮助您获取进行这些评估所需的数据。
回答by Marcin
Google Testability Explorerchecks for example for singletons and other static things which are bad smells in design. Metricsis an Eclipse plugin that measures almost every code metric known to mankind. I used and can easily recommend both.
谷歌可测试性资源管理器检查例如单例和其他在设计中是难闻的静态事物。Metrics是一个 Eclipse 插件,几乎可以测量人类已知的所有代码指标。我使用过并且可以轻松推荐两者。
回答by Csaba_H
Sonartries to identify "hot spots" of complexity and maintainability combining the results of various open source tools (including PMD and Findbugs). It integrates well with Maven and CI servers (especially Hudson).
Sonar尝试结合各种开源工具(包括 PMD 和 Findbugs)的结果来识别复杂性和可维护性的“热点”。它与 Maven 和 CI 服务器(尤其是 Hudson)很好地集成在一起。
EDITby extraneon
由 extraneon编辑
There is a Sonar site availablewhere a lot of open source projects are analyzed. I think this shows quite good how much rules get applied,and how far the drill down goes. You can of course also disable rules you don't find that interesting.
有一个 Sonar 站点可用,其中分析了许多开源项目。我认为这很好地显示了应用了多少规则以及深入研究的程度。您当然也可以禁用您觉得不那么有趣的规则。
Hereis an explanation of the metrics.
这里是对指标的解释。
回答by Jens Schauder
I never used it, but I found this rather interesting and promissing:
我从未使用过它,但我发现这很有趣且很有希望:
http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/
http://erik.doernenburg.com/2008/11/how-toxic-is-your-code/
And I used this one and found it extremely helpful, because the nice visualization of dependencies
我使用了这个并发现它非常有用,因为依赖关系的很好的可视化
http://www.headwaysoftware.com/products/structure101/index.php
http://www.headwaysoftware.com/products/structure101/index.php
回答by Daniel F. Thornton
回答by Patrick from NDepend team
The tool NDependfor .NET code will let you analyze many dimensions of the code complexity including code metrics like: Cyclomatic Complexity, Nesting Depth, Lack Of Cohesion of Methods, Coverage by Tests...
用于 .NET 代码的工具NDepend将让您分析代码复杂性的多个维度,包括代码指标,例如:圈复杂度、嵌套深度、方法缺乏内聚力、测试覆盖率...
...including dependencies analysis and including Code Rules over LINQ Queries (CQLinq)dedicated to ask, what is complex in my code, and to write rule. Around 200 default Code Rulesare provided. They concern anti-patterns like the Singleton, detection of threading problems, detection of coupling problems like UI layer shouldn't use directly DB types...
...包括依赖关系分析和LINQ 查询 (CQLinq) 上的代码规则,专门用于询问、我的代码中的复杂性以及编写规则。提供了大约200 个默认代码规则。它们涉及像Singleton这样的反模式、线程问题的检测、UI 层等耦合问题的检测不应直接使用 DB 类型......
A while back, I wrote an article to summarize several dimensions of code complexity: Fighting Fabricated Complexity
前阵子,我写了一篇文章总结了代码复杂度的几个维度: Fighting Fabricated Complexity

