IDEA 巧用 Postfix Completion 讓碼速起飛

白菜Java自習室 涵蓋核心知識express

1. 情景展現

自從作 Java 開發以後,IDEA 編輯器是不可少的。 在 IDEA 編輯器中,有不少高效的代碼補全功能,尤爲是 Postfix Completion 功能,能夠讓編寫代碼更加的流暢。markdown

Postfix completion 本質上也是代碼補全,它比 Live Templates 在使用上更加流暢一些,咱們能夠看一下下面的這張圖。編輯器

2. 設置界面

能夠經過以下的方法打開 Postfix 的設置界面,並開啓 Postfix。spa

3. 經常使用的 Postfix 模板

3.1. boolean 變量模板

!: Negates boolean expressioncode

//before
public class Foo {
     void m(boolean b) {
         m(b!);
     }
 }
 
//after
public class Foo {
    void m(boolean b) {
        m(!b);
    }
}
複製代碼

if: Checks boolean expression to be 'true'orm

//before
public class Foo {
    void m(boolean b) {
        b.if
    }
}

//after
public class Foo {
    void m(boolean b) {
        if (b) {

        }
    }
}
複製代碼

else: Checks boolean expression to be 'false'.開發

//before
public class Foo {
    void m(boolean b) {
        b.else
    }
}

//after
public class Foo {
    void m(boolean b) {
        if (!b) {

        }
    }
}
複製代碼

3.2. array 變量模板

for: Iterates over enumerable collection.it

//before
public class Foo {
    void m() {
        int[] values = {1, 2, 3};
        values.for
    }
}

//after
public class Foo {
    void m() {
        int[] values = {1, 2, 3};
        for (int value : values) {

        }
    }
}
複製代碼

fori: Iterates with index over collection.io

//before
public class Foo {
    void m() {
        int foo = 100;
        foo.fori
    }
}

//after
public class Foo {
    void m() {
        int foo = 100;
        for (int i = 0; i < foo; i++) {

        }
    }
}
複製代碼

3.3. 基本類型模板

opt: Creates Optional object.table

//before
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
  intValue.opt
  doubleValue.opt
  longValue.opt
  objValue.opt
}

//after
public void m(int intValue, double doubleValue, long longValue, Object objValue) {
  OptionalInt.of(intValue)
  OptionalDouble.of(doubleValue)
  OptionalLong.of(longValue)
  Optional.ofNullable(objValue)
}
複製代碼

sout: Creates System.out.println call.

//before
public class Foo {
  void m(boolean b) {
    b.sout
  }
}

//after
public class Foo {
  void m(boolean b) {
      System.out.println(b);
  }
}
複製代碼

3.4. Object 模板

nn: Checks expression to be not-null.

//before
public class Foo {
    void m(Object o) {
        o.nn
    }
}
//after
public class Foo {
    void m(Object o) {
        if (o != null){

        }
    }
}
複製代碼

null: Checks expression to be null.

//before
public class Foo {
    void m(Object o) {
        o.null
    }
}
//after
public class Foo {
    void m(Object o) {
        if (o != null){

        }
    }
}
複製代碼

notnull: Checks expression to be not-null.

//before
public class Foo {
    void m(Object o) {
        o.notnull
    }
}
//after
public class Foo {
    void m(Object o) {
        if (o != null){

        }
    }
}
複製代碼

val: Introduces variable for expression.

//before
public class Foo {
    void m(Object o) {
        o instanceof String.var
    }
}

//after
public class Foo {
    void m(Object o) {
        boolean foo = o instanceof String;
    }
}
複製代碼

3.5. 其餘模板

new: Inserts new call for the class.

//before
Foo.new

//after
new Foo()
複製代碼

return: Returns value from containing method.

//before
public class Foo {
    String m() {
        "result".return
    }
}
//after
public class Foo {
    String m() {
        return "result";
    }
}
複製代碼
相關文章
相關標籤/搜索