Rule
规则
AWT
Avoid using java.awt.peer interface directly
不要直接使用java.awt.peer接口
Casting
Avoid returning java.lang.Object,downcast to specific type instead
不要直接返回java.lang.Object,尽量转换成具体的类型
Avoid casting primitive types to lower precision
不要把原始类型转换成更底层的类型
Cloneable
Implement Cloneable when you override clone()
覆盖clone()方法时,最好实现Cloneable接口
Avoid implementing Cloneable without overring clone()
如果没有覆盖clone()方法,就不要实现Cloneable接口
Always invoke super.clone() from a clone()
在clone()方法中总是调用super.clone()
Comparison
Avoid using java.lang.Class.getName() to compare classes
不要使用java.lang.Class.getName()来比较类
Always override both java.lang.Object.equals() and java.lang.Object.hashCode()
要覆盖java.lang.Object.equals()和 java.lang.Object.hashCode()
Always use instanceof in an equals() method implementation
在实现equals()方法时要使用instanceof操作符
Always place constants on the left side of the equals()
要把常量放在equals()方法的左边
Avoid using == and != for java.lang.Object comparisons
不要使用== 和 !=来比较ava.lang.Object
Conditional
Avoid using negation in if else conditions
不要在if…else条件中使用否定判断
Avoid using if/else statements for short conditions
不要对短的条件使用if…else语句
Constructors
Avoid calling an abstract method in a constructor in an abstract class
不要在抽象类的构造函数中调用抽象方法
Avoid calling an overridable method in a constructor
不要在构造函数中调用可覆盖的方法
Declaration
Avoid C-styntax when declaring arrays
在声明数组时不要使用C风格的语法
Avoid using explicit string literals, declare constants instead
不要直接使用明文字符串,把它声明为常量
Avoid using interfaces or abstract classes just to declare common constants
不要仅仅为了声明通用的常量而使用接口或抽象类
Avoid declaring variables of basic and array types in the same statement
不要在同一个语句中声明基本类型变量和数组变量
Avoid declaring multiple variables in a single statement
不要在一个语句中声明多个变量
Exceptions
Avoid catching a checked exception that is not thrown within the try{} block
不要捕获try{}语句块中没有抛出的Exception
Avoid using instanceof operator to check the type of exception in the catch clause
不要在cathch语句中使用instanceof操作符来判断Exception的类型
Always list specific checked exceptions in the throws clause
在throws语句中列举出所有确定的Exception
Initialization
Avoid changing the value of a method parameter
不要改变方法的参数
Avoid chaining assignment operators
不要进行连锁赋值操作
Always initialize static fields
要初始化静态域
Loop
Avoid assigning loop control variables in the body of for loops
不要在循环体中给控制循环的变量赋值
Avoid using continue statements
尽量不要使用continue语句
Avoid a for loop without a condition
不要使用没有条件控制的for循环
Avoid a for loop without an initializer and an incrementor
不要使用没有初始状态和增加器for循环
Null
Avoid returning null instead of empty array
不要用null代替空数组返回
Avoid returning null instead of Enumeration
不要用null代替枚举类型返回
Avoid returning null instead of Iterator
不要用null代替迭代返回
Portability
Avoid hard coding file separators when creating a java.io.File
在创建java.io.File时,不要硬编码文件分隔符
Avoid hard coding \\n and \\r as line separators
不要使用硬编码\\n 和\\r作为换行符
Avoid calling java.lang.System.getenv()
不要调用java.lang.System.getenv()
Reflection
Avoid calling java.lang.Class.getDeclaredField()
不要调用java.lang.Class.getDeclaredField()
Avoid calling java.lang.Class.getDeclaredMethod()
不要调用java.lang.Class.getDeclaredMethod()
Avoid calling java.lang.Class.getField()
不要调用java.lang.Class.getField()
Avoid calling java.lang.Class.getMethod()
不要调用java.lang.Class.getMethod()
Serialization
Always declare readObject() and writeObject() as private in externalizable or serializable classes
在Externalizable和Serializable接口的实现类中,要把readObject() 和writeObject()方法声明为私有的
Always declare readResolve() and writeReplace() as protected in externalizable or serializable classes
在Externalizable和Serializable接口的实现类中,要把readResolve () 和writeReplace ()方法声明为保护的
Avoid declaring non-transient fields of non-serializable type inside of a serializable class
不要在Serializable接口的实现类中声明非Serializable类型的非transient 域
Always create a static final serialVersionUID field in serializable classes
在Serializable接口的实现类中创建一个static final的VersionUID
Avoid declaring transient fields in non-serializable classes
不要在非Serializable接口的实现类中声明transient域
Statement
Avoid empty if statements and empty loops
避免空的if语句和空循环
Always surround if and loop statements with curly braces
把if和循环语句放在括号中
Switch
Avoid switch statements with few branches, use if else instead
用if...else语句替换分支很少的switch语句
Always provide break at the end of every case statement
在每个case语句后面加上break语句
Always provide the default label in switch statements
为switch语句提供defaul标签
Threads
Avoid extending java.lang.Thread
不要扩展Thread类
Avoid using java.lang.Object.notify(), use java.lang.Object.notifyAll() instead
用java.lang.Object.notifyAll()代替java.lang.Object.notify()
Avoid overriding a synchronized method with a non-synchronized one
不要用synchronized方法覆盖非synchronized方法
Avoid using synchronized modifier in method declaration
不要在方法的声明中添加synchronized修饰符
Avoid using "this" as a lock in synchronized statements
Avoid using "this" as a lock in synchronized statements
不要在synchronized语句使用this
Avoid using java.lang.Object.wait() outside of a while loop
不要在while循环之外使用java.lang.Object.wait()
Avoid using while sleep, use wait notify instead
用wait notify替代while sleep
相关资源:敏捷开发V1.0.pptx