PMD规则之Controversial

     PMD规则

UnnecessaryConstructor: This rule detects when a constructor is not necessary; i.e., when there's only one constructor, it's public, has an empty body, and takes no arguments.

非必要的构造器:本规则检查不必要的构造器,例如:只存在一个公共的,空方法体的,无参的构造器

 

NullAssignment: Assigning a "null" to a variable (outside of its declaration) is usually bad form. Some times, the assignment is an indication that the programmer doesn't completely understand what is going on in the code. NOTE: This sort of assignment may in rare cases be useful to encourage garbage collection. If that's what you're using it for, by all means, disregard this rule :-)

Null赋值:将null赋值给变量(在声明之外)常常是不好的形式。某些时候这种赋值表示程序员没有想好代码的下一步该做什么。

备注:当你需要把变量赋值为null提示垃圾收集器去进行垃圾收集时这是有用的,那么请忽略这个规则

 

OnlyOneReturn: A method should have only one exit point, and that should be the last statement in the method.

只有一个返回:一个方法应该有且只有一处返回点,且应该是方法的最后一条语句。

 

UnusedModifier: Fields in interfaces are automatically public static final, and methods are public abstract. Classes or interfaces nested in an interface are automatically public and static (all nested interfaces are automatically static). For historical reasons, modifiers which are implied by the context are accepted by the compiler, but are superfluous.

无用的修饰符:在接口中定义的域自动为public static final的,方法自动是public abstract的,接口中嵌套的类或接口自动是public static的。由于历史原因,上下文暗示的修饰符是被编译器接受的,但是是多余的。

 

AssignmentInOperand: Avoid assignments in operands; this can make code more complicated and harder to read.

在操作中赋值:避免在操作中赋值;这会使代码复杂并且难以阅读

public class Foo {

 public void bar() {

  int x = 2;

  if ((x = getX()) == 3) {

   System.out.println("3!");

  }

 }

 private int getX() {

  return 3;

 }

}

 

AtLeastOneConstructor: Each class should declare at least one constructor.

至少有一个构造器:每个类应该至少声明一个构造器

 

DontImportSun: Avoid importing anything from the 'sun.*' packages. These packages are not portable and are likely to change.

不要引入Sun包:避免从”sun.*”引入任何类,这些包不是轻便的而且可能更改

 

 SuspiciousOctalEscape: A suspicious octal escape sequence was found inside a String literal. The Java language specification (section 3.10.6) says an octal escape sequence inside a literal String shall consist of a backslash followed by: OctalDigit | OctalDigit OctalDigit | ZeroToThree OctalDigit OctalDigit Any octal escape sequence followed by non-octal digits can be confusing, e.g. "/038" is interpreted as the octal escape sequence "/03" followed by the literal character "8".

令人迷惑的八进制转义序列:在字符串字面量中出现令人迷惑的八进制转义序列。Java语言规范(3.10.6)讲到:在一个字面量字符串中的八进制转义序列应该包含一个反斜杠,后续可以是:八进制数字|八进制数字八进制数字|0~3 八进制数字八进制数字的格式。所以任何八进制转义序列后面紧跟着一个非八进制的数字就可能造成迷惑,例如:“/038

 

CallSuperInConstructor: It is a good practice to call super() in a constructor. If super() is not called but another constructor (such as an overloaded constructor) is called, this rule will not report it.

在构造器中调用super():在构造器中调用super()方法是很好的做法.如果没有调用super(),但是调用了另外的构造器,那么这个规则不会报告出来。

 

UnnecessaryParentheses: Sometimes expressions are wrapped in unnecessary parentheses, making them look like a function call.

不必要的圆括号:有时候表达式被包在一个不必要的圆括号中,使它们看起来像是一个函数调用

public class Foo {

      boolean bar() {

          return (true);

      }

  }

 

DefaultPackage: Use explicit scoping instead of the default package private level.

翻译  默认的包:使用明确的范围代替默认的包私有的级别

 

BooleanInversion: Use bitwise inversion to invert boolean values - it's the fastest way to do this. See http://www.javaspecialists.co.za/archive/newsletter.do?issue=042&locale=en_US for specific details

布尔转换:使用按位转换来转换布尔值-这是最快的方法,参考:http://www.javaspecialists.co.za/archive/newsletter.do?issue=042&locale=en_US for specific details

public class Foo {

 public void main(bar) {

  boolean b = true;

  b = !b; // slow

  b ^= true; // fast

 }

}

 

DataflowAnomalyAnalysis: The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow. From those informations there can be found various problems. 1. UR - Anomaly: There is a reference to a variable that was not defined before. This is a bug and leads to an error. 2. DU - Anomaly: A recently defined variable is undefined. These anomalies may appear in normal source text. 3. DD - Anomaly: A recently defined variable is redefined. This is ominous but don't have to be a bug.

数据流异常分析:数据流分析是跟踪本地的变量定义与否及在数据流中不同路径的变量引用。由此可以发现多种问题:1.UR-异常:指向一个之前没有定义的变量,这是bug且可导致错误2.DU-异常:一个刚刚定义的变量是未定义的。这些异常可能出现在普通的源代码文本中3.DD-异常:一个刚刚定义的变量重新定义。这是不好的但并非一定是个bug

注:这个规则实在有点绕,具体含义我也不是很透彻理解!

 

public class Foo {

    public void foo() {

       int buz = 5;

       buz = 6; // redefinition of buz -> dd-anomaly

       foo(buz);

       buz = 2;

    } // buz is undefined when leaving scope -> du-anomaly

}

 

AvoidFinalLocalVariable: Avoid using final local variables, turn them into fields.

翻译  避免Final类型的本地变量:避免使用final类型的本地变量,将它们转为类域

public class MyClass {

    public void foo() {

        final String finalLocalVariable;

    }

}

 

AvoidUsingShortType: Java uses the 'short' type to reduce memory usage, not to optimize calculation. In fact, the jvm does not have any arithmetic capabilities for the short type: the jvm must convert the short into an int, do the proper caculation and convert the int back to a short. So, the use of the 'short' type may have a greater impact than memory usage.

避免使用short类型:Java使用’short’类型来减少内存开销,而不是优化计算。事实上,JVM不具备short类型的算术能力:jvm必须将short类型转化为int类型,然后进行适当的计算再把int类型转回short类型。因此,和内存开销比起来使用’short’类型会对性能有更大的影响

 

AvoidUsingVolatile: Use of the keyword 'volatile' is general used to fine tune a Java application, and therefore, requires a good expertise of the Java Memory Model. Moreover, its range of action is somewhat misknown. Therefore, the volatile keyword should not be used for maintenance purpose and portability.

避免使用Volatile:使用关键字’volatile’一般用来调整一个Java应用,因此,需要一个专业的Java内存模型。此外,它的作用范围一定程度上是令人误解的。因此,volatile关键字应该不要被用做维护和移植的目的。

 

AvoidUsingNativeCode: As JVM and Java language offer already many help in creating application, it should be very rare to have to rely on non-java code. Even though, it is rare to actually have to use Java Native Interface (JNI). As the use of JNI make application less portable, and harder to maintain, it is not recommended.

避免使用本地代码:jvmJava语言已经提供了很多创建应用程序的帮助,依赖非Java代码应该是非常罕见的。即使如此,事实上必须使用Java本地接口也是罕见的。因为使用JNI使得应用可移植性降低,而且难以维护,所以是不推荐的。

 

AvoidAccessibilityAlteration: Methods such as getDeclaredConstructors(), getDeclaredConstructor(Class[]) and setAccessible(), as the interface PrivilegedAction, allow to alter, at runtime, the visilibilty of variable, classes, or methods, even if they are private. Obviously, no one should do so, as such behavior is against everything encapsulation principal stands for.

避免改变访问控制:getDeclaredConstructors(), getDeclaredConstructor(Class[]) setAccessible(),还有PrivilegedAction接口,允许在运行时改变变量、类和方法的可见性,甚至它们是私有的。显然,这是不应该的,因为这种动作违背了封装原则

 

DoNotCallGarbageCollectionExplicitly: Calls to System.gc(), Runtime.getRuntime().gc(), and System.runFinalization() are not advised. Code should have the same behavior whether the garbage collection is disabled using the option -Xdisableexplicitgc or not. Moreover, "modern" jvms do a very good job handling garbage collections. If memory usage issues unrelated to memory leaks develop within an application, it should be dealt with JVM options rather than within the code itself.

不要显示的调用垃圾收集器:调用System.gc(), Runtime.getRuntime().gc(), System.runFinalization()是不推荐的。当垃圾收集器使用配置项-Xdisableexplicitgc关闭时,使用代码可以同样进行垃圾收集。此外,现代JVM对于垃圾收集工作做得很棒。当开发一个应用时内存使用的影响无关于内存泄露时,垃圾收集应该交给JVM配置项进行管理而非代码本身

 

NPathComplexity: The NPath complexity of a method is the number of acyclic execution paths through that method. A threshold of 200 is generally considered the point where measures should be taken to reduce complexity.

翻译  n条路径复杂度:NPath复杂度是一个方法中各种可能的执行路径总和,一般把200作为考虑降低复杂度的临界点

 

ExcessiveMethodLength: Violations of this rule usually indicate that the method is doing too much. Try to reduce the method size by creating helper methods and removing any copy/pasted code.

翻译  方法太长:这种违例就是方法中做了太多事,通过创建辅助方法或移除拷贝/粘贴的代码试着减小方法的规模

 

ExcessiveParameterList: Long parameter lists can indicate that a new object should be created to wrap the numerous parameters. Basically, try to group the parameters together.

太多的参数:过长的参数列表表明应该创建一个新的对象包装众多的参数值,就是把参数组织到一起

 

ExcessiveClassLength: Long Class files are indications that the class may be trying to do too much. Try to break it down, and reduce the size to something manageable.

太长的类:太长的类文件表明类试图做太多的事,试着分解它,减少到易于管理的规模

 

CyclomaticComplexity: Complexity is determined by the number of decision points in a method plus one for the method entry. The decision points are 'if', 'while', 'for', and 'case labels'. Generally, 1-4 is low complexity, 5-7 indicates moderate complexity, 8-10 is high complexity, and 11+ is very high complexity.

秩复杂性:由ifwhileforcase labels等决策点确定的复杂度,1-4是低复杂度,5-7为中,810是高复杂度,11以上是非常高

 

ExcessivePublicCount: A large number of public methods and attributes declared in a class can indicate the class may need to be broken up as increased effort will be required to thoroughly test it.

过多的公共成员:一个类中如果声明了大量的公共方法和属性表明类需要分解,因为想完全测试这个类需要大量的努力。

 

TooManyFields: Classes that have too many fields could be redesigned to have fewer fields, possibly through some nested object grouping of some of the information. For example, a class with city/state/zip fields could instead have one Address field.

太多的域:类包含太多域可以被重新设计为包含更少的域,可以通过将一些信息组织为嵌套类。比如:一个类包含了city/state/zip域,可以用一个Address域组织这三个域

 

NcssMethodCount: This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

NCSS方法代码计算:这个规则采用NCSS(非注释代码块)算法计算给定的方法(不含构造方法)的代码行数。NCSS忽略代码中的注释并且计算实际代码行数。用这种算法,一行单独的代码被计算为1. (也同时忽略空行)

 

NcssTypeCount: This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines of code for a given type. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

NCSS类代码计算:这个规则采用NCSS(非注释代码块)算法计算给定类型的代码行数。NCSS忽略代码中的注释并且计算实际代码行数。用这种算法,一行单独的代码被计算为1.(也同时忽略空行)

 

NcssConstructorCount: This rule uses the NCSS (Non Commenting Source Statements) algorithm to determine the number of lines of code for a given constructor. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.

翻译  NCSS构造器代码计算:这个规则适用NCSS(非注释代码块)算法计算给定的构造方法的代码行数。NCSS忽略代码中的注释并且计算实际代码行数。用这种算法,一行单独的代码被计算为1.(也同时忽略空行)

 

TooManyMethods: A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.

翻译  太多的方法:类中包含太多方法可能需要重构,以减低复杂度和获取更加细粒度的对象

       

转载:

http://blog.csdn.net/jack0511/article/details/5344286

http://blog.csdn.net/jack0511/article/details/5344274

猜你喜欢

转载自lbxc.iteye.com/blog/1882538
PMD