可變字段不該該是public static程序員
不合規編程
public interface MyInterface { public static String [] strings; // Noncompliant } public class A { public static String [] strings1 = {"first","second"}; // Noncompliant public static String [] strings2 = {"first","second"}; // Noncompliant public static List<String> strings3 = new ArrayList<>(); // Noncompliant // ... }
合規數組
private static final String [] COLUMN_NAMES = new String[]{"date","customerNumber","customerName", "account","emailAdress","mobilePhoneNumber","emailStatus"}; public static List<String> getColumnNames() { return Collections.unmodifiableList(Arrays.asList(COLUMN_NAMES)); }
問題分析微信
請注意,使可變字段(例如數組)爲final將阻止從新分配變量,但這樣作不會影響數組內部狀態的可變性ide
能夠改變的類型若是很是有必要聲明爲 ‘public static’ 能夠經過上述 Collections.unmodifiableList 方式實現。性能
public static 屬性應是常量this
合規代碼編碼
public class Greeter { public static Foo foo = new Foo(); public static String realmOplate = "this is name"; ... }
不合規代碼spa
public class Greeter { public static final Foo FOO = new Foo(); ... } private static String realmOplate = "this is name"; public static String getRealmOplate() { return realmOplate; } public static void setRealmOplate(String realm) { realmOplate = realm; }
分析指針
在大多數狀況下,public static 在多個對象之間共享狀態的一種指望。可是使用這種方法,任何對象均可以在共享狀態下作任何想作的事情,例如將其設置爲null。
只有設置爲了final時纔會防止共享值被修改。
類中的成員屬性不該該共有訪問。
不合格代碼
public class MyClass { public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked public String firstName; // Noncompliant }
合規代碼
public class MyClass { public static final int SOME_CONSTANT = 0; // Compliant - constants are not checked private String firstName; // Compliant public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } }
問題分析
公共類變量字段不遵照封裝原理,而且具備三個主要缺點:
沒法添加其餘行爲,例如驗證。
內部表示形式已公開,之後不能更改。
成員值可能會在代碼中的任何位置發生更改,而且可能不符合程序員的假設。
經過使用私有屬性和訪問器方法(設置和獲取),能夠防止未經受權的修改。
不合規代碼
logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages
合規代碼
logger.log(Level.DEBUG, "Something went wrong:{} ",message)
分析
日誌在輸出的狀況下支持一個字符串輸出,沒有必要經過+生成多個字符串,浪費性能。
不合規代碼
public String toString () { if (this.collection.isEmpty()) { return null; // Noncompliant } else { // ... @Override public Object clone() { try { return (ContractQuery) super.clone(); } catch (CloneNotSupportedException e) { } return null; }
合規
public String toString () { if (this.collection.isEmpty()) { return ""; } else { // ... @Override public Object clone() throws CloneNotSupportedException { return (ContractQuery) super.clone(); }
分析
當toString clone 返回Null時,容易產生空指針問題。
Java 語言中修飾符的位置順序。
1. Annotations
2. public
3. protected
4. private
5. abstract
6. static
7. final
8. transient
9. volatile
10. synchronized
11. native
12. strictfp
不合規代碼
public final static String HOME= "home";
合規代碼
public static final String HOME = "home";
分析
應根據約定俗稱的規範進行編碼,代碼不單單是給機器用,但倒是給人看的。寫給機器的能用的不叫本事,寫出人人都看的懂的纔是能耐。
因此聲明過時的方法,類都有與之對應的代替方式。
反射中經常使用的
//newInstance() 已過時 ContractDto contract = contractClass.newInstance(); //代替方式 contractClass.getDeclaredConstructor().newInstance()
lambda 表達式中一個輸入參數沒有必要加括號
不合規代碼
(x) -> x * 2
合規代碼
x -> x * 2
代碼編程長路漫漫,吾將上下而求索。使用sonar改進代碼,漂亮的代碼使人嚮往,猶如一位漂亮精緻的姑娘,讓人目不轉睛,愛不釋手。
作個有追求的程序員仍是有必要的。
頭條號
微信號