主要问题列表:
问题 (格式:问题名字+问题出现的次数) | 解释 | 补充说明 | |
---|---|---|---|
Resources should be closed2 | Java's garbage collection cannot be relied on to clean up everything. Specifically, connections, streams, files and other classes that implement theCloseableinterface or it's super-interface,AutoCloseable, must be manually closed after creation. Failure to do so will result in a resource leak which could bring first the application and then perhaps the box it's on to their knees. |
资源未关闭,打开发现有两处用到的 IO 流没有关闭。 |
|
Conditions should not unconditionally evaluate to "TRUE" or to "FALSE"1 |
if/else 判断里出现了重复判断,比如在if(a>10) 的执行体里面又判断if(a<0) ,而后者肯定不会是true 。 |
||
Exception handlers should preserve the original exception13 | 处理异常的时候应该保留原始的异常情况,不要直接来个catch(Exception e) 了事。 |
||
Throwable.printStackTrace(...) should not be called7 | 不应该直接调用e.printStackTrace() ,而是用Loggers 来处理(就是打Log )。Loggers 的优势是:Users are able to easily retrieve the logs.The format of log messages is uniform and allow users to browse the logs easily.
|
||
Instance methods should not write to "static" fields6 | 不要用实例方法改变静态成员,理想情况下,静态变量只通过同步的静态方法来改变。 | ||
"public static" fields should be constant1 | 公共静态成员应该加上final ,也就是public static final 一般不分家。 |
||
Thread.run() and Runnable.run() should not be called directly1 | 不应该直接调用Thread 和Runnable 对象的run 方法,直接调用run 会使得run 方法执行在当前线程,失去了开启新线程的意义。但有时候可能会这样做,见例子。 |
||
Generic exceptions should never be thrown1 | |||
Class variable fields should not have public accessibility64 | 类变量不要设置为public ,而是设为private ,再提供get 和set 方法。 |
||
Sections of code should not be "commented out"30 | 不要再注释中出现大量的代码段,会使代码可读性变差。 | ||
Package declaration should match source file directory19 | |||
Utility classes should not have public constructors16 | 工具类不应该有公共的构造器,也就是说至少要有一个private 的构造器,如果没有,默认的构造器是public 的。 |
||
The diamond operator ("<>") should be used12 | 在定义集合的时候,等号右边的<>内不需要再写上元素类型,直接空着就行。 | 正确的顺序如下所示: 静态成员变量→成员变量→构造器→方法 |
|
Lambdas and anonymous classes should not have too many lines9 | Anonymous classes and lambdas (with Java 8) are a very convenient and compact way to inject a behavior without having to create a dedicated class. But those anonymous inner classes and lambdas should be used only if the behavior to be injected can be defined in a few lines of code, otherwise the source code can quickly become unreadable.anonymous class number of lines : at most 20 |
Lambdas 表达式和匿名内部类不要写太多行,一般最多写20行。 |
|
Anonymous inner classes containing only one method should become lambdas8 | 只包含一个方法的匿名内部类应该写成 Lambdas 表达式的形式,增强代码可读性。 |
||
Try-with-resources should be used8 | 用Try-with-resources 的形式取代try/catch/finally 的形式。 |
||
Methods should not be empty7 | 不要写空方法,除非这种情况:An abstract class may have empty methods, in order to provide default implementations for child classes.
|
||
Source files should not have any duplicated blocks7 | 源文件中不要出现任何重复的代码段或行或字符串等。 | ||
"switch case" clauses should not have too many lines6 |
switch case 每个case 里面的代码不要太长,太长的话可以考虑写个方法代替,主要是为了增强代码可读性。 |
||
Nested blocks of code should not be left empty6 | 嵌套代码块不要是空的,比如 if( a > 0 )<> { doSomething() } else { } ,这时候应该把后面的else{} 去掉。 |
||
Methods should not be too complex6 | 方法不要太复杂,否则难以理解和维护。 | ||
Unused private fields should be removed5 | 没有使用的private的成员变量应该移除掉。 | ||
Dead stores should be removed5 | 没有用到的本地变量或其他死存储应该移除掉,也就是写方法的时候,定义的变量如果后来发现根本用不到,要记得删掉那行代码。 | ||
"switch" statements should end with a "default" clause4 |
switch 语句应该以default 结束,这是一种defensive programming 思想。 |
||
Unused method parameters should be removed4 | 没有用到的方法参数应该移除掉。 | ||
Control flow statements "if", "for", "while", "switch" and "try" should not be nested too deeply4 |
if/for/while/try 这样的嵌套不要太复杂。 |
||
Useless parentheses around expressions should be removed to prevent any misunderstanding3 | 没有意义的括号不要随便加,以免造成误解,比如"="两边对象类型是相同的,就不要强转。 | ||
"for" loop stop conditions should be invariant3 |
for 循环的结果条件不能是变量,而应该是常量。 |
||
"static" members should be accessed statically2 |
static 成员是与类、静态方法相联系的。 |
||
Catches should be combined2 | |||
Primitives should not be boxed just for "String" conversion2 | 不要使用 4+这样的方式将int值转变为字符串,而是使用Integer.toString(4) 这样的方式。就像Integer.parseInt("我是字符串")这样,不要偷懒。 |
||
Classes should not be empty2 | 不要写空类。 | ||
Unused local variables should be removed2 | 没有用到的本地变量要删掉。 | ||
"entrySet()" should be iterated when both the key and value are needed2 | 直接看英文更直接:When only the keys from a map are needed in a loop, iterating the keySet makes sense. But when both the key and the value are needed, it's more efficient to iterate theentrySet, which will give access to both the key and value, instead. 也就是说,如果只需要Map 的Key ,那么直接iterate 这个Map 的keySet 就可以了,但是如果Key 和value 都需要,就iterate 这个Map 。 |
||
Method parameters, caught exceptions and foreach variables should not be reassigned2 | 方法参数/捕获的异常/foreach 的变量不应该被重新赋值。 |
||
Collection.isEmpty() should be used to test for emptiness2 | 当判断集合是否为空的时候,不要使用if (myCollection.size() == 0) 这样的方式,而是使用if (myCollection.isEmpty() 这样的方式,后者性能更高。 |
||
Standard outputs should not be used directly to log anything2 | 标准输出不直接打印任何东西,也就是打log 的时候,不要使用System.out.println("My Message") 这样的方式,而是使用logger.log("My Message") 这种方式。 |
||
Generic wildcard types should not be used in return parameters1 | 通配符不应该出现在返回声明中。比如这句:List <? extends Animal>getAnimals(){...} , 我们无法知道“是否可以把a Dog, a Cat 等加进去”,等之后用到这个方法的时候,我们没必要去考虑这种问题(前面引号里面的)。 |
||
Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used1 | 不要使用同步的Vector/HashTable/Stack/StringBuffer 等。在早期,出于线程安全问题考虑,Java API 提供了这些类。但是同步会极大影响性能,即使是在同一个线程中使用他们。通常可以这样取代:ArrayList or LinkedList instead of Vector ,Deque instead of Stack ,HashMap instead of Hashtable ,StringBuilder instead of StringBuffer.
|
||
Exit methods should not be called | 尽量不要调用system.exit() 方法。 |
||
Local Variables should not be declared and then immediately returned or thrown7 | 本地变量如果赋值之后直接return 了,那就直接return 本地变量的赋值语句。 |
||
Field names should comply with a naming convention6 | 命名要规范。 | ||
Local variable and method parameter names should comply with a naming convention6 | 命名要规范。 | ||
String literals should not be duplicated5 | 字符串不应该重复,如果多次用到同一字符串,建议将该字符串定义为字符串常量,再引用。 | ||
Return of boolean expressions should not be wrapped into an "if-then-else" statement3 | 不要写if ( a > 4 ) { return false } else { return true } 这样的代码,直接写return a > 4 。 |
||
Static non-final field names should comply with a naming convention2 | 命名要规范。 | ||
Modifiers should be declared in the correct order2 | 修饰符等要按约定俗成的顺序书写 ,例如:写成public static 而不是static public 。 |
||
The members of an interface declaration or class should appear in a pre-defined order2 | 根据Oracle 定义的Java 代码规范中,不同代码的出现位置应该如下所示:class and instance variables--Constructors--Methods
|
||
Array designators "[]" should be on the type, not the variable2 | 数组的括号要写在类型后面,而不是变量后面,例如 int[] a 而不是int a[] 。 |
||
Multiple variables should not be declared on the same line1 | 不要在同一行定义多个变量。 | ||
"switch" statements should have at least 3 "case" clauses1 | 当至少有3种或者3种以上的情况时,才考虑用switch ,否则用if/else 的形式。 |
||
Overriding methods should do more than simply call the same method in the super class1 | 既然在子类中重写了父类的某个方法,那就再这个方法中做些与父类方法不同的事情,否则没必要重写。 | ||
Statements should be on separate lines1 | 错误:if(someCondition) doSomething() ;正确:if(someCondition) { doSomething() }
|
||
Method names should comply with a naming convention1 | 命名要规范。 | ||
"TODO" tags should be handle |
TODO 标签要及时处理,该做的事情不要忘了做。 |