摘抄自:http://pmd.sourceforge.net/rules/optimizations.html
一、Optimization Rules:最佳规则
1、LocalVariableCouldBeFinal:A local variable assigned only once can be declared final.
2、MethodArgumentCouldBeFinal:A method argument that is never assigned can be declared final.
3、AvoidInstantiatingObjectsInLoops:Detects when a new object is created inside a loop
public class Something {
public static void main( String as[] ) {
for (int i = 0; i < 10; i++) {
Foo f = new Foo(); //Avoid this whenever you can it's really expensive
}
}
}
5、UseArrayListInsteadOfVector:ArrayList is a much better Collection implementation than Vector.
6、UseArraysAsList:The java.util.Arrays class has a "asList" method that should be used when you want to create a new List from an array of objects. It is faster than executing a loop to copy all the elements of the array one by one.
public void foo(Integer[] ints) {
// could just use Arrays.asList(ints)
List l= new ArrayList(10);
for (int i=0; i< 100; i++) {
l.add(ints[i]);
}
for (int i=0; i< 100; i++) {
l.add(a[i].toString()); // won't trigger the rule
}
}
7、AvoidArrayLoops:Instead of copying data between two arrays, use System.arraycopy method.
public void bar() {
int[] a = new int[10];
int[] b = new int[10];
for (int i=0;i<10;i++) {
b[i]=a[i];
}
}
// this will trigger the rule
for (int i=0;i<10;i++) {
b[i]=a[c[i]];
}
}
8 、AddEmptyStirng:Finds empty string literals which are being added. This is an inefficient(无效) way to convert any type to a String.
String s = "" + 123; // bad
String t = Integer.toString(456); // ok
二、Basic Rules:The Basic Ruleset contains a collection of good practices which everyone should follow.
1、EmptyCatchBlock:
public void doSomething() {
try {
FileInputStream fis = new FileInputStream("/tmp/bugger");
} catch (IOException ioe) {
// not good
}
2、JumbledIncrementer:Avoid jumbled loop incrementers - it's usually a mistake, and it's confusing even if it's what's intended.
for (int i = 0; i < 10; i++) {
for (int k = 0; k < 20; i++) {
System.out.println("Hello");
}
}
3、UnnecessaryConventionTemporary:Avoid unnecessary temporaries when converting primitives to Strings
// this wastes an object
String foo = new Integer(x).toString();
// this is better
return Integer.toString(x);
3、DoubleCheckingLocking:Partially(部分的) created objects can be returned by the Double Checked Locking pattern when used in Java. An optimizing(最佳的) JRE may assign a reference to the baz variable before it creates the object the reference is intended to point to
Object baz;
Object bar() {
if(baz == null) { //baz may be non-null yet not fully created
synchronized(this){
if(baz == null){
baz = new Object();
}
}
}
return baz;
}
4、ReturnFromFinalBlock:Avoid returning from a finally block - this can discard exceptions.
5、UnnecessaryReturn
public void bar() {
int x = 42;
return;
}
6、UnnecessaryReturn
7、UnConditionIfStatement:Do not use "if" statements that are always true or always false.
8、BooleanInstantiation:Avoid instantiating Boolean objects; you can reference Boolean.TRUE, Boolean.FALSE, or call Boolean.valueOf() instead.
9、UnnnessaryFinalModifier:
public final class Foo {
// This final modifier is not necessary, since the class is final
// and thus, all methods are final
private final void foo() {
}
}
10、CollapsiableIfStament:Sometimes two 'if' statements can be consolidated(合并) by separating their conditions with a boolean short-circuit operator(短路&&).
void bar() {
if (x) {
if (y) {
// do stuff
}
}
}
11、ClassCastExceptionWithToArray:
public static void main(String[] args) {
Collection c=new ArrayList();
Integer obj=new Integer(1);
c.add(obj);
// this would trigger the rule (and throw a ClassCastException if executed)
Integer[] a=(Integer [])c.toArray();
// this wouldn't trigger the rule
Integer[] b=(Integer [])c.toArray(new Integer[c.size()]);
}
12、BrokenNullCheck:The null check is broken since it will throw a NullPointerException itself. It is likely that you used || instead of && or vice versa(反之亦然).
// should be &&
if (string!=null || !string.equals(""))
return string;
// should be ||
if (string==null && string.equals(""))
return string;
13、AvoidUsingOctalValue:Integer literals should not start with zero. Zero means that the rest of literal will be interpreted(理解) as an octal value(八进制).
public class Foo {
int i = 012; // set i with 10 not 12
int j = 010; // set j with 8 not 10
k = i * j; // set k with 80 not 120
}
分享到:
相关推荐
自己整理的PMD检查的各个规则说明,其中打○的是我自己用的规则,大家自己按实际需要选择吧,有不对的地方请多多指教。
PMD是一款采用BSD协议发布的Java程序代码检查工具。该工具可以做到检查Java代码中是否含有未使用的变量、是否含有空的抓取块、是否含有不必要的对象等。该软件功能强大,扫描效率高,是Java程序员debug的好帮手。 ...
PMD是一款采用BSD协议发布的Java程序代码检查工具。该工具可以做到检查Java代码中是否含有未使用的变量、是否含有空的抓取块、是否含有不必要的对象等。该软件功能强大,扫描效率高,是Java程序员debug的好帮手。 ...
PMD是一款开源的静态代码检测工具,可以检查代码的质量问题,文件是对PMD检测规则进行说明,解压密码:PMD。
pmd-bin 静态代码分析工具 ...PMD具有许多内置检查(以PMD术语,规则),在我们的“规则”参考中针对每种语言进行了记录。 我们还支持广泛的API来编写您自己的规则,您可以使用Java或作为独立的XPath查询来执行。
PMD是一款静态代码分析工具,它能够自动检测各种潜在缺陷以及不安全或未优化的代码。 PMD更多地是集中在预先检测缺陷上,它提供了高度可配置的丰富规则集,用户可以方便配置对待特定项目使用那些规则
PMD是一款采用BSD协议发布的Java程序代码检查工具。该工具可以做到检查Java代码中是否含有未使用的变量、是否含有空的抓取块、是否含有不必要的对象等。该软件功能强大,扫描效率高,是Java程序员debug的好帮手。 ...
PMD是一种分析Java代码错误的工具。与其他分析工具不同的是,PMD通过静态分析获知代码错误。也就是说,在不运行Java程序的情况下报告错误。...此外,用户还可以自己定义规则,检查Java代码是否符合某些特定的编码规范
PMD正是这样一种工具,可以直接使用它自带的规则(当然也可以使用自己的规则)对Java源程序进行分析找出程序存在的问题,可以很大程度上的减轻代码检查工作的繁琐,为项目组今后的维护和开发工作起到指导的作用。...
PMD(Programming Mistake Detector)是一款流行的插件,用于检测 Java 代码中的错误和不良编程实践。下面我们将介绍如何在 Eclipse 中配置和使用 PMD 插件。 安装 PMD 插件 要在 Eclipse 中安装 PMD 插件,需要...
自己在网上找资料整理的doc文档,主要介绍了如何使用pmd工具进行代码的自动化检查,以规避一些潜在的问题并找出代码的逻辑错误。
PMD 插件for Eclipse,PMD是一种开源分析Java代码错误的工具。与其他分析工具不同的是,PMD通过静态分析获知代码错误。...此外,用户还可以自己定义规则,检查Java代码是否符合某些特定的编码规范。
使用ML4SC和PMD和CheckStyle规则和指标的Java代码质量检查程序的PoC 过程 下载热门的Github项目并提取Java文件 主题-Java: : java o s = stars(例如最多和最少的星星) 趋势: : since monthly spoken_language...
PMD是一款采用BSD协议发布的Java程序代码检查工具。该工具可以做到检查Java代码中是否含有未使用的变量、是否含有空的抓取块、是否含有不必要的对象等。该软件功能强大,扫描效率高,是Java程序员debug的好帮手
AndroidLintPlus A demo to custom your Lint rules ...使用checkstyle,pmd,findbug共同提高代码质量。 最后一句:程序员何苦为难程序员呢 Feature 1.JavaChineseStringDetector:check chinese string in java f
Java代码检查工具 PMD Pmd 它是一个基于静态规则集的Java源码分析器,该软件功能强大,扫描效率高,是Java程序员debug的好帮手。 它可以识别出潜在的如下问题: – 可能的bug——空的try/catch/finally/...
1.不遵循代码标准 sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具规范代码编写 2. 潜在的缺陷 sonar可以通过PMD,CheckStyle,Findbugs等等代码规则检测工具检测出潜在的缺陷 3. 糟糕的复杂度分布 文件...
Android hodgepodge静态代码质量检查工具gradle插件。 (。>﹏<。) Gradle project build.gradle dependencies { com . camnter . gradle . plugin : static - analysis - plugin : 1.0.6 } module build....
摘要:作者Veera Sundar在清理代码工作时发现一些常见的违规编码,因此,... 这份列表没有依据任何规则或顺序,所有的这些都是通过代码质量工具包括CheckStyle,FindBugs和PMD检查出。一起来看下: 一、Eclips
PMD,CheckStyle和FindBugs的自定义规则以及规则的单元测试; 工具,将来自各个插件的报告合并到摘要报告中。必需品 第三党static-code-analysis-config提供的示例检查( MethodLimitCheck , CustomClass