1. 程式人生 > 實用技巧 >這樣規範寫程式碼,同事直呼“666”

這樣規範寫程式碼,同事直呼“666”

一、MyBatis 不要為了多個查詢條件而寫 1 = 1

當遇到多個查詢條件,使用where 1=1 可以很方便的解決我們的問題,但是這樣很可能會造成非常大的效能損失,因為添加了 “where 1=1 ”的過濾條件之後,資料庫系統就無法使用索引等查詢優化策略,資料庫系統將會被迫對每行資料進行掃描(即全表掃描) 以比較此行是否滿足過濾條件,當表中的資料量較大時查詢速度會非常慢;此外,還會存在SQL 注入的風險。

反例:

<select id="queryBookInfo" parameterType="com.tjt.platform.entity.BookInfo" resultType="
java.lang.Integer"> select count(*) from t_rule_BookInfo t where 1=1 <if test="title !=null and title !='' "> AND title = #{title} </if> <if test="author !=null and author !='' "> AND author = #{author} </if> </select>

正例:

<select id="queryBookInfo" parameterType="
com.tjt.platform.entity.BookInfo" resultType="java.lang.Integer"> select count(*) from t_rule_BookInfo t <where> <if test="title !=null and title !='' "> title = #{title} </if> <if test="author !=null and author !='' "> AND author = #{author} </if> </where> </select
>

UPDATE 操作也一樣,可以用標記代替 1=1。

二、迭代entrySet() 獲取Map 的key 和value

當迴圈中只需要獲取Map 的主鍵key時,迭代keySet() 是正確的;但是,當需要主鍵key 和取值value 時,迭代entrySet() 才是更高效的做法,其比先迭代keySet() 後再去通過get 取值效能更佳。

反例:

//Map 獲取value 反例:
HashMap<String, String> map = new HashMap<>();
for (String key : map.keySet()){
    String value = map.get(key);
}

正例:

//Map 獲取key & value 正例:
HashMap<String, String> map = new HashMap<>();
for (Map.Entry<String,String> entry : map.entrySet()){
 String key = entry.getKey();
 String value = entry.getValue();
}

三、使用Collection.isEmpty() 檢測空

使用Collection.size() 來檢測是否為空在邏輯上沒有問題,但是使用Collection.isEmpty() 使得程式碼更易讀,並且可以獲得更好的效能;除此之外,任何Collection.isEmpty() 實現的時間複雜度都是O(1) ,不需要多次迴圈遍歷,但是某些通過Collection.size() 方法實現的時間複雜度可能是O(n)

反例:

LinkedList<Object> collection = new LinkedList<>();
if (collection.size() == 0){
 System.out.println("collection is empty.");
}

正例:

LinkedList<Object> collection = new LinkedList<>();
if (collection.isEmpty()){
    System.out.println("collection is empty.");
}

//檢測是否為null 可以使用CollectionUtils.isEmpty()
if (CollectionUtils.isEmpty(collection)){
    System.out.println("collection is null.");

}

四、初始化集合時儘量指定其大小

儘量在初始化時指定集合的大小,能有效減少集合的擴容次數,因為集合每次擴容的時間複雜度很可能時O(n),耗費時間和效能。

反例:

//初始化list,往list 中新增元素反例:
int[] arr = new int[]{1,2,3,4};
List<Integer> list = new ArrayList<>();
for (int i : arr){
 list.add(i);
}

正例:

//初始化list,往list 中新增元素正例:
int[] arr = new int[]{1,2,3,4};
//指定集合list 的容量大小
List<Integer> list = new ArrayList<>(arr.length);
for (int i : arr){
    list.add(i);
}

五、使用StringBuilder 拼接字串

一般的字串拼接在編譯期Java 會對其進行優化,但是在迴圈中字串的拼接Java 編譯期無法執行優化,所以需要使用StringBuilder 進行替換。

反例:

//在迴圈中拼接字串反例
String str = "";
for (int i = 0; i < 10; i++){
    //在迴圈中字串拼接Java 不會對其進行優化
    str += i;
}

正例:

//在迴圈中拼接字串正例
String str1 = "Love";
String str2 = "Courage";
String strConcat = str1 + str2;  //Java 編譯器會對該普通模式的字串拼接進行優化
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++){
   //在迴圈中,Java 編譯器無法進行優化,所以要手動使用StringBuilder
    sb.append(i);
}

六、若需頻繁呼叫Collection.contains 方法則使用Set

在Java 集合類庫中,List的contains 方法普遍時間複雜度為O(n),若程式碼中需要頻繁呼叫contains 方法查詢資料則先將集合list 轉換成HashSet 實現,將O(n) 的時間複雜度將為O(1)。

反例:

//頻繁呼叫Collection.contains() 反例
List<Object> list = new ArrayList<>();
for (int i = 0; i <= Integer.MAX_VALUE; i++){
    //時間複雜度為O(n)
    if (list.contains(i))
    System.out.println("list contains "+ i);
}

正例:

//頻繁呼叫Collection.contains() 正例
List<Object> list = new ArrayList<>();
Set<Object> set = new HashSet<>();
for (int i = 0; i <= Integer.MAX_VALUE; i++){
    //時間複雜度為O(1)
    if (set.contains(i)){
        System.out.println("list contains "+ i);
    }
}

七、使用靜態程式碼塊實現賦值靜態成員變數

對於集合型別的靜態成員變數,應該使用靜態程式碼塊賦值,而不是使用集合實現來賦值。

反例:

/賦值靜態成員變數反例
private static Map<String, Integer> map = new HashMap<String, Integer>(){
    {
        map.put("Leo",1);
        map.put("Family-loving",2);
        map.put("Cold on the out side passionate on the inside",3);
    }
};
private static List<String> list = new ArrayList<>(){
    {
        list.add("Sagittarius");
        list.add("Charming");
        list.add("Perfectionist");
    }
};

正例:

//賦值靜態成員變數正例
private static Map<String, Integer> map = new HashMap<String, Integer>();
static {
    map.put("Leo",1);
    map.put("Family-loving",2);
    map.put("Cold on the out side passionate on the inside",3);
}

private static List<String> list = new ArrayList<>();
static {
    list.add("Sagittarius");
    list.add("Charming");
    list.add("Perfectionist");
}

十三、返回空陣列和集合而非 null

反例:

//返回null 反例
public static Result[] getResults() {
    return null;
}

public static List<Result> getResultList() {
    return null;
}

public static Map<String, Result> getResultMap() {
    return null;
}

正例:

//返回空陣列和空集正例
public static Result[] getResults() {
    return new Result[0];
}

public static List<Result> getResultList() {
    return Collections.emptyList();
}

public static Map<String, Result> getResultMap() {
    return Collections.emptyMap();
}

十五、列舉的屬性欄位必須是私有且不可變

列舉通常被當做常量使用,如果列舉中存在公共屬性欄位或設定欄位方法,那麼這些列舉常量的屬性很容易被修改;理想情況下,列舉中的屬性欄位是私有的,並在私有建構函式中賦值,沒有對應的Setter 方法,最好加上final 修飾符。

反例:

public enum SwitchStatus {
    // 列舉的屬性欄位反例
    DISABLED(0, "禁用"),
    ENABLED(1, "啟用");

    public int value;
    private String description;

    private SwitchStatus(int value, String description) {
        this.value = value;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

正例:

public enum SwitchStatus {
    // 列舉的屬性欄位正例
    DISABLED(0, "禁用"),
    ENABLED(1, "啟用");

    // final 修飾
    private final int value;
    private final String description;

    private SwitchStatus(int value, String description) {
        this.value = value;
        this.description = description;
    }

    // 沒有Setter 方法
    public int getValue() {
        return value;
    }

    public String getDescription() {
        return description;
    }
}

十六、tring.split(String regex)部分關鍵字需要轉譯

使用字串String 的plit 方法時,傳入的分隔字串是正則表示式,則部分關鍵字(比如 .[]()| 等)需要轉義。

反例:

// String.split(String regex) 反例
String[] split = "a.ab.abc".split(".");
System.out.println(Arrays.toString(split));   // 結果為[]

String[] split1 = "a|ab|abc".split("|");
System.out.println(Arrays.toString(split1));  // 結果為["a", "|", "a", "b", "|", "a", "b", "c"]

正例:

// String.split(String regex) 正例
// . 需要轉譯
String[] split2 = "a.ab.abc".split("\\.");
System.out.println(Arrays.toString(split2));  // 結果為["a", "ab", "abc"]

// | 需要轉譯
String[] split3 = "a|ab|abc".split("\\|");
System.out.println(Arrays.toString(split3));  // 結果為["a", "ab", "abc"]