總結咱們在開發中常見的代碼問題,同時將sonar中的問題也概括了進來java
1、異常處理中的未打印或者拋出異常信息api
這個主要是沒有將異常信息打印出來,又或者異常沒有拋出。好比咱們在action中,異常信息是打印成日誌,而service中是將異常信息拋出,按照這樣作了,就不會再有這類的錯誤。less
l Sonaride
Either log or rethrow this exception.測試 |
l 錯誤示例ui
略this
l 處理方案spa
將異常經過日誌打印或者拋出debug
2、使用非同步的類來代替同步的類調試
非同步的類在執行效率上會高於同步的類,因此在不是必須同步的狀況下,建議使用非同步類。
l Sonar
Replace the synchronized class "StringBuffer" by an unsynchronized one such as "StringBuilder". |
l 錯誤示例
略
l 處理方案
ArrayList or LinkedList instead of Vector Deque instead of Stack HashMap instead of Hashtable StringBuilder instead of StringBuffer |
3、移除沒有用到的包、變量、方法
l Sonar
Remove this useless assignment to local variable xxx |
l 錯誤示例
略
l 處理方案
移除無用的代碼
4、增長有效註釋,移除無用註釋
咱們在開發中,須要增長有效註釋,移除無用的無效註釋。有時候,在修改代碼的時候也會將之前的代碼進行註釋,防止下次修改的時候再拿來參考或者使用。這個也不推薦,由於時間長了,這類的殭屍代碼也比較難清除,而且影響代碼的閱讀
l Sonar
This block of commented-out lines of code should be removed. |
l 錯誤示例
略
l 處理方案
移除無用的註釋
5、繼承的方法須要添加@Override
l Sonar
Add the "@Override" annotation above this method signature |
l 錯誤示例
略
l 處理方案
在方法上方添加 @Override
6、嵌套不要超過三層
在寫代碼的時候,if/for/while/switch/try等代碼的嵌套層數建議不要超過3層。當超過3層以後,代碼的可讀性和可維護性都會變的不好,同時若是作單測也會變的很艱難。
l Sonar
Refactor this code to not nest more than 3 if/for/while/switch/try statements. |
l 錯誤示例
if (condition1) { // Compliant - depth = 1 /* ... */ if (condition2) { // Compliant - depth = 2 /* ... */ for(int i = 0; i < 10; i++) { // Compliant - depth = 3, not exceeding the limit /* ... */ if (condition4) { // Noncompliant - depth = 4 if (condition5) { // Depth = 5, exceeding the limit, but issues are only reported on depth = 4 /* ... */ } return; } } } } |
l 處理方案
減小嵌套層數
7、方法塊的圈複雜度太高
l Sonar
The Cyclomatic Complexity of this method xxx is 11 which is greater than 10 authorized. |
l 錯誤示例
略
l 處理方案
減小嵌套,減小代碼行數,避免一個方法過於冗長和沉重,提升閱讀性和可維護性。
8、應合併可摺疊的if語句
l Sonar
Merge this if statement with the enclosing one. |
l 錯誤示例
if (file != null) { if (file.isFile() || file.isDirectory()) { /* ... */ } } |
l 處理方案
if (file != null && isFileOrDirectory(file)) { /* ... */ }
private static boolean isFileOrDirectory(File file) { return file.isFile() || file.isDirectory(); } |
9、集合的是否爲空集合的判斷
在java的一些api中,提供了相似字符串、集合等常見對象的取值和判斷的方法,咱們要加以運用。
l Sonar
Use isEmpty() to check whether the collection is empty or not. |
l 錯誤示例
if (myCollection.size() == 0) { // Noncompliant /* ... */ } |
l 處理方案
if (myCollection.isEmpty()) { // Compliant /* ... */ } |
10、整形/長整形轉字符串的方法
在java的一些api中,提供了相似字符串、集合等常見對象的取值和判斷的方法,咱們要加以運用。
l Sonar
Use "Long.toString" instead. |
l 錯誤示例
int myInt = 4; String myIntString = new Integer(myInt).toString(); // Noncompliant; creates & discards an Integer object myIntString = Integer.valueOf(myInt).toString(); // Noncompliant myIntString = 4 + ""; // Noncompliant |
l 處理方案
int myInt = 4; String myIntString = Integer.toString(myInt); |
11、 BigDecimal的取值方法
在java的一些api中,提供了相似字符串、集合等常見對象的取值和判斷的方法,咱們要加以運用。
l Sonar
Use "BigDecimal.valueOf" instead. |
l 錯誤示例
double d = 1.1;
BigDecimal bd1 = new BigDecimal(d); // Noncompliant; see comment above BigDecimal bd2 = new BigDecimal(1.1); // Noncompliant; same result |
l 處理方案
double d = 1.1;
BigDecimal bd1 = BigDecimal.valueOf(d); BigDecimal bd2 = BigDecimal.valueOf(1.1); |
12、 使用新的變量來代替傳入的變量
方法中傳入的變量,通常狀況下,咱們認爲是不可修改的,一旦可修改的話,在測試的時候不容易發現問題是調用方發生的錯誤仍是方法自己發生的錯誤,這也是有的方法的傳入參數爲何要定義爲final類型的緣由。
l Sonar
Use isEmpty() to check whether the collection is empty or not. |
l 錯誤示例
class MyClass { public String name;
public MyClass(String name) { name = name; // Noncompliant - useless identity assignment }
public int add(int a, int b) { a = a + b; // Noncompliant
/* additional logic */
return a; // Seems like the parameter is returned as is, what is the point? }
public static void main(String[] args) { MyClass foo = new MyClass(); int a = 40; int b = 2; foo.add(a, b); // Variable "a" will still hold 40 after this call } } |
l 處理方案
class MyClass { public String name;
public MyClass(String name) { this.name = name; // Compliant }
public int add(int a, int b) { return a + b; // Compliant }
public static void main(String[] args) { MyClass foo = new MyClass(); int a = 40; int b = 2; foo.add(a, b); } }} |
十3、 使用正確的類型轉換方法
類型轉換的方法有不少種方式,咱們須要使用更合適的。
l Sonar
Use "Integer.parseInt" for this string-to-int conversion |
l 錯誤示例
String myNum = "12.2";
float f = new Float(myNum).floatValue(); // Noncompliant; creates & discards a Float |
l 處理方案
String myNum = "12.2";
float f = Float.parseFloat(myNum); |
十4、 不要使用System.out或者System.err
避免使用System.out和Sytem.err,而使用日誌的方式輸出,同時注意日誌的級別,若是隻是本身調試使用的話,不要使用Info以上的級別,使用debug就能夠了。
l Sonar
Replace this usage of System.out or System.err by a logger. |
l 錯誤示例
System.out.println("My Message"); // Noncompliant |
l 處理方案
logger.info("My Message"); |
十5、 字符串比較的左邊放常量,右邊放變量
l Sonar
Move the "0" string literal on the left side of this string comparison. |
l 錯誤示例
String myString = null;
System.out.println("Equal? " + myString.equals("foo")); // Noncompliant - will raise a NPE System.out.println("Equal? " + (myString != null && myString.equals("foo"))); // Noncompliant - null check could be removed |
l 處理方案
System.out.println("Equal?" + "foo".equals(myString)); // Compliant - properly deals with the null case |