Java Checkstyle 与 PMD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/184563/
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
Checkstyle vs. PMD
提问by John Stauffer
We are introducing static analysis tools into the build system for our Java product. We are using Maven2 so Checkstyleand PMDintegration come for free. However it looks like there is a large overlap in functionality between these two tools, in terms of enforcing basic style rules.
我们将静态分析工具引入到我们 Java 产品的构建系统中。我们正在使用 Maven2,因此Checkstyle和PMD集成是免费的。然而,在执行基本样式规则方面,这两个工具之间的功能似乎有很大的重叠。
Is there a benefit from utilizing both of these? I don't want to maintain 2 tools if one will work. If we choose one, which one should we use and why?
使用这两种方法有好处吗?如果一个工具行得通,我不想维护 2 个工具。如果我们选择一种,我们应该使用哪一种,为什么?
We are also planning on using FindBugs. Are there other static analysis tools we should look at?
我们还计划使用 FindBugs。还有其他我们应该考虑的静态分析工具吗?
Update:Consensus seems to be that PMD is preferred over CheckStyle. I don't see a solid reason to use both, and I don't want to maintain 2 sets of rule files, so we will probably aim for PMD exclusively. We'll also be bringing in FindBugs, and perhaps, eventually, Macker to enforce architectural rules.
更新:共识似乎是 PMD 优于 CheckStyle。我认为没有充分理由同时使用两者,而且我不想维护 2 组规则文件,因此我们可能会专门针对 PMD。我们还将引入 FindBugs,也许最终会引入 Macker 来执行架构规则。
采纳答案by Chris Vest
You should definitely use FindBugs. In my experience, the false-positive rate is very low, and even the least-critical warnings it reports are worth addressing to some extent.
你绝对应该使用FindBugs。根据我的经验,误报率非常低,即使它报告的最不重要的警告也值得在一定程度上解决。
As for Checkstyle vs. PMD, I would not use Checkstyle since it is pretty much only concerned with style. In my experience, Checkstyle will report on a ton of things that are completely irrelevant. PMD on the other hand is also able to point out questionable coding practices and its output is generally more relevant and useful.
至于 Checkstyle 与 PMD,我不会使用 Checkstyle,因为它几乎只与样式有关。根据我的经验,Checkstyle 会报告大量完全不相关的事情。另一方面,PMD 也能够指出有问题的编码实践,其输出通常更相关和有用。
回答by anjanb
PMD is what I find more people referring to. Checkstyle was what people were referring to 4 years ago but I believe PMD is maintained more continuously and what other IDEs/plugins choose to work with.
PMD 是我发现更多人提到的。Checkstyle 是 4 年前人们所指的,但我相信 PMD 的维护更加持续,其他 IDE/插件选择使用什么。
回答by Steve Moyer
I would echo the comment that PMD is the more current product for Java style/convention checking. With respect to FindBugs, many commercial development groups are using Coverity.
我会回应这样的评论,即 PMD 是 Java 样式/约定检查的最新产品。关于 FindBugs,许多商业开发团队都在使用 Coverity。
回答by Steve Moyer
Both softwares are useful. Checkstyle will help you during your programming by checking your coding stylei.e braces, naming etc. Simple things but very numerous!
这两个软件都很有用。Checkstyle 将在您的编程过程中通过检查您的编码风格(即大括号、命名等)来帮助您。简单的事情却非常多!
PMD will help you by checking more complicate rules like during the design of your classes, or for more special problems like implementing correctly the clone function. Simply, PMD will check your programming style
PMD 将通过检查更复杂的规则(例如在类设计期间)或更特殊的问题(例如正确实现克隆功能)来帮助您。简单地说,PMD 会检查你的编程风格
However, both softwares suffers from similar rules sometimes bad explained. With a bad configuration, you may check things twice or two opposite things i.e "Remove useless constructors" and "Always one constructor".
然而,这两种软件都受到类似的规则的影响,有时解释得不好。对于错误的配置,您可能会检查两次或两次相反的事情,即“删除无用的构造函数”和“始终只有一个构造函数”。
回答by Ilya Kochetov
We use both:
我们同时使用:
- Checkstyle to make sure that everyone in the team write code in a similar maner
- PMD to find problematic code areas and next refactoring targets
- Checkstyle 确保团队中的每个人都以相似的方式编写代码
- PMD 查找有问题的代码区域和下一个重构目标
回答by Alex Miller
I find Checkstyle and PMD are best for enforcing style issues and simple obvious coding bugs. Although I've found that I like using Eclipse and all the warnings it provides better for that purpose. We enforce stuff by using shared preferences and marking them as actual errors. That way, they never get checked in in the first place.
我发现 Checkstyle 和 PMD 最适合解决风格问题和简单明显的编码错误。尽管我发现我喜欢使用 Eclipse 以及它为此目的提供的所有警告。我们通过使用共享首选项并将它们标记为实际错误来强制执行。这样一来,他们就永远不会被登记在首位。
What I would strongly and enthusiastically recommend is using FindBugs. Because it works at the bytecode level it can check things that are impossible at the source level. While it spits out its fair share of junks, it has found many actual and important bugs in our code.
我强烈而热情地推荐的是使用 FindBugs。因为它在字节码级别工作,所以它可以检查在源级别不可能的事情。虽然它吐出了相当多的垃圾,但它在我们的代码中发现了许多实际和重要的错误。
回答by Greg Mattes
Both tools are configurable and can do just about the same things. That said, if we're talking about out-of-the-box stuff, there is a great deal of overlap, but there are distinct rules/checks as well. For example, Checkstyle has stronger support for checking Javadoc and finding magic numbers, to name a couple. Additionally, Checkstyle has an "import control" feature that looks similar to the functionality of Macker (I've not used Macker).
这两种工具都是可配置的,可以做几乎相同的事情。也就是说,如果我们谈论的是开箱即用的东西,会有很多重叠,但也有不同的规则/检查。例如,Checkstyle 对检查 Javadoc 和查找幻数有更强大的支持,举几个例子。此外,Checkstyle 有一个“导入控制”功能,看起来类似于 Macker 的功能(我没有使用过 Macker)。
If there are things that are important to you that Checkstyle does out-of-the-box that PMD doesn't, you might consider a minimal Checkstyle configuration with only those checks. Then institute a policy that the Checkstyle configuration cannot grow, simply remove checks as you implement similar functionality with, say, custom PMD rules.
如果有些事情对您来说很重要,Checkstyle 可以开箱即用而 PMD 没有,您可以考虑仅包含这些检查的最小 Checkstyle 配置。然后制定 Checkstyle 配置不能增长的策略,只需在您使用自定义 PMD 规则实现类似功能时删除检查即可。
Also consider that if you decide that the Checkstyle "import control" feature covers what you wanted from Macker, then you could implement PMD/Checkstyle instead of PMD/Macker. Either way it's two tools, but with Checkstyle, you'd get the stuff that PMD doesn't do out-of-the-box "for free."
还要考虑的是,如果您决定 Checkstyle“导入控制”功能涵盖了您想要从 Macker 获得的内容,那么您可以实施 PMD/Checkstyle 而不是 PMD/Macker。无论哪种方式,它都是两个工具,但是使用 Checkstyle,您可以“免费”获得 PMD 无法立即执行的功能。
回答by Greg Mattes
Checkstyle and PMD both are good at checking coding standards and are easy to extend. But PMD has additional rules to check for cyclomatic complexity,Npath complexity,etc which allows you write healthy code.
Checkstyle 和 PMD 都擅长检查编码标准并且易于扩展。但是 PMD 有额外的规则来检查圈复杂度、Npath 复杂度等,这允许您编写健康的代码。
Another advantage of using PMD is CPD (Copy/Paste Detector).It finds out code duplication across projects and is not constrained to JAVA.It works for JSP too. Neal Ford has a good presentation on Metrics Driven Agile Development, which talks about many tools that are helpful for Java/Java EE Development
使用 PMD 的另一个优点是 CPD(Copy/Paste Detector)。它可以发现跨项目的代码重复,并且不受 JAVA 的限制。它也适用于 JSP。Neal Ford 有一篇关于Metrics Driven Agile Development的精彩演讲,其中谈到了许多对 Java/Java EE 开发有帮助的工具
回答by janasainik
PMD is the finest tool when compare with checkstyles. Checkstyles may not have the capability to analyse the code while PMD offering many features to do so! Offcourse PMD has not released rules for javadoc, comments, indentations and etc. And by the way i am planning to implement these rules.......thanx
与 checkstyles 相比,PMD 是最好的工具。Checkstyles 可能没有分析代码的能力,而 PMD 提供了许多功能来这样做!Offcourse PMD 还没有发布关于 javadoc、评论、缩进等的规则。顺便说一下,我计划实施这些规则......thanx
回答by Naveen
If your primary place of use is while developing in eclipse, then CodePro from Instantiations will be best. Earlier it was a commercial tool, but now Google bought Instantiations so CodePro analytix is free now.
如果您的主要使用场所是在 Eclipse 中进行开发,那么来自 Instantiations 的 CodePro 将是最佳选择。早些时候它是一个商业工具,但现在谷歌购买了 Instantiations,所以 CodePro analytix 现在是免费的。
Check out http://code.google.com/javadevtools/download-codepro.html
查看 http://code.google.com/javadevtools/download-codepro.html