Sonar代码规则之TOP30详解
- 1. 规则简述:String literals should not be duplicated.
- 2. 规则简述:Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used.
- 3. 规则简述:Cognitive Complexity of methods should not be too high.
- 4. 规则简述:Dead stores should be removed.
- 5. 规则简述:Generic exceptions should never be thrown.
- 6. 规则简述:Nested blocks of code should not be left empty.
- 7. 规则简述:Unused "private" methods should be removed.
- 8. 规则简述:Conditionals should start on new lines.
- 9. 规则简述:Standard outputs should not be used directly to log anything.
- 10. 规则简述:String function use should be optimized for single characters.
- 11. 规则简述:Constant names should comply with a naming convention.
- 12. 规则简述:Utility classes should not have public constructors.
- 13. 规则简述:Constructors should not be used to instantiate "String", "BigInteger", "BigDecimal" and primitive-wrapper classes.
- 14. 规则简述:Try-catch blocks should not be nested.
- 15. 规则简述:Resources should be closed.
- 16. 规则简述:Unused method parameters should be removed.
- 17. 规则简述:Methods should not be empty.
- 18. 规则简述:Collapsible "if" statements should be merged.
- 19. 规则简述:Unused "private" fields should be removed.
- 20. 规则简述:A conditionally executed single line should be denoted by indentation.
- 21. 规则简述:Null pointers should not be dereferenced.
- 22. 规则简述:Boolean expressions should not be gratuitous.
- 23. 规则简述:Methods should not have too many parameters.
- 24. 规则简述:Constants should not be defined in interfaces.
- 25. 规则简述:Local variables should not shadow class fields.
- 26. 规则简述:"@Override" should be used on overriding and implementing methods.
- 27. 规则简述:Credentials should not be hard-coded.
- 28. 规则简述:"switch" statements should have "default" clauses.
- 29. 规则简述:Instance methods should not write to "static" fields.
- 30. 规则简述:Child class methods named for parent class methods should be overrides.
1. 规则简述:String literals should not be duplicated.
字符串文本不应重复。
规则内容:Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences. On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
例外情况:To prevent generating some false-positives, literals having less than 5 characters are excluded.
违规代码示例:
合规解决方案:
2. 规则简述:Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used.
线程安全的类Vector,Hashtable,Stack和StringBuffer不应该被使用。
规则内容:Early classes of the Java API, such as Vector, Hashtable and StringBuffer, were synchronized to make them thread-safe. Unfortunately, synchronization has a big negative impact on performance, even when using these collections from a single thread.
It is better to use their new unsynchronized replacements:
- ArrayList or LinkedList instead of Vector
- Deque instead of Stack
- HashMap instead of Hashtable
- StringBuilder instead of StringBuffer
例外情况:Use of those synchronized classes is ignored in the signatures of overriding methods.
@Override
public Vector getCats(){...}
违规代码示例:
Vector cats = new Vector();
合规解决方案:
ArrayList cats = new ArrayList();
3. 规则简述:Cognitive Complexity of methods should not be too high.
方法的认知复杂性不应太高。
规则内容:Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
4. 规则简述:Dead stores should be removed.
没用的存储应该被移除。
规则内容:A dead store happens when a local variable is assigned a value that is not read by any subsequent instruction. Calculating or retrieving a value only to then overwrite it or throw it away, could indicate a serious error in the code. Even if it’s not an error, it is at best a waste of resources. Therefore all calculated values should be used.
例外情况:This rule ignores initializations to -1, 0, 1, null, true, false and “”.
违规代码示例:
合规解决方案:
5. 规则简述:Generic exceptions should never be thrown.
通用异常不应抛出。
规则内容:Using such generic exceptions as Error, RuntimeException, Throwable, and Exception prevents calling methods from handling true, system-generated exceptions differently than application-generated errors.
例外情况:Generic exceptions in the signatures of overriding methods are ignored, because overriding method has to follow signature of the throw declaration in the superclass. The issue will be raised on superclass declaration of the method (or won’t be raised at all if superclass is not part of the analysis).
Generic exceptions are also ignored in the signatures of methods that make calls to methods that throw generic exceptions.
违规代码示例:
合规解决方案:
6. 规则简述:Nested blocks of code should not be left empty.
嵌套代码块不应是空的。
规则内容:Most of the time a block of code is empty when a piece of code is really missing. So such empty block must be either filled or removed.
例外情况:When a block contains a comment, this block is not considered to be empty unless it is a synchronized block. synchronized blocks are still considered empty even with comments because they can still affect program flow.
违规代码示例:
7. 规则简述:Unused “private” methods should be removed.
没用的私有方法应该被移除。
规则内容:private methods that are never executed are dead code: unnecessary, inoperative code that should be removed. Cleaning out dead code decreases the size of the maintained codebase, making it easier to understand the program and preventing bugs from being introduced.
Note that this rule does not take reflection into account, which means that issues will be raised on private methods that are only accessed using the reflection API.
例外情况:This rule doesn’t raise any issue on annotated methods.
违规代码示例:
合规解决方案:
8. 规则简述:Conditionals should start on new lines.
条件表达式应该起始新行。
规则内容:Code is clearest when each statement has its own line. Nonetheless, it is a common pattern to combine on the same line an if and its resulting then statement. However, when an if is placed on the same line as the