After working for a period of time, I feel that my code is not standardized, with a lot of redundancy and chaos. May I ask how to improve the code specification specifically? Don’t panic. Here are 10 tips for eating

MyBatis do not write 1 = 1 for multiple query conditions

When we encounter multiple query conditions, using where 1=1 can be very convenient to solve the problem, but it may cause a very large performance loss, because the database system can not use indexes and other query optimization strategies after adding “where 1=1” filtering conditions. The database system will be forced to scan each row of data (i.e., full table scan) to compare whether the row meets the filtering conditions. The query speed will be very slow when the amount of data in the table is large. There is also the risk of SQL injection.

Example:

<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>Copy the code

Is:

<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>Copy the code

The same goes for the UPDATE operation, which can be marked 1=1 instead.

Iterates entrySet() to retrieve the Map key and value

Iterating keySet() is correct when the loop only needs to get the Map’s primary key; However, when the primary key and value are needed, iterating through entrySet() is more efficient than iterating through keySet() and then iterating through get.

Example:

Counter example: HashMap<String, String> Map = new HashMap<>(); for (String key : map.keySet()){ String value = map.get(key); }Copy the code

Is:

Example: HashMap<String, String> Map = new HashMap<>(); for (Map.Entry<String,String> entry : map.entrySet()){ String key = entry.getKey(); String value = entry.getValue(); }Copy the code

Use collection.isEmpty () to detect empty

Using collection.size () to detect empty is logically fine, but using collection.isEmpty () makes the code easier to read and gives better performance; In addition, any implementation of collection.isempty () has a time complexity of O(1) and does not require multiple loops, but some implementations with collection.size () may have a time complexity of O(n).

Example:

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

Is:

LinkedList<Object> collection = new LinkedList<>(); if (collection.isEmpty()){ System.out.println("collection is empty."); IsEmpty () if (collectionUtils.isempty (collection)){system.out.println ("collection is null."); }Copy the code

When initializing a collection, try to specify its size

If possible, specify the size of the collection during initialization, which can effectively reduce the number of capacity expansion of the collection, because the time complexity of each capacity expansion of the collection is likely to be O(N), which costs time and performance.

Example:

Int [] arr = new int[]{1,2,3,4}; List<Integer> list = new ArrayList<>(); for (int i : arr){ list.add(i); }Copy the code

Is:

Int [] arr = new int[]{1,2,3,4}; List<Integer> list = new ArrayList<>(arr.length); for (int i : arr){ list.add(i); }Copy the code

Use StringBuilder to concatenate strings

Normal string concatenation is optimized by Java at compile time, but concatenation of strings in loops cannot be optimized at compile time, so StringBuilder is used instead.

Example:

// To concatenate a String in a loop: String STR = ""; for (int i = 0; i < 10; I ++){// String concatenation in a loop is not optimized by Java. }Copy the code

Is:

String str1 = "Love"; String str2 = "Courage"; String strConcat = str1 + str2; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10; I ++){// In the loop, the Java compiler cannot optimize, so use StringBuilder sb. Append (I) manually; }Copy the code

6. Set is used if the collection. contains method is frequently called

In the Java set class library, the general time complexity of the contains method of List is O(n). If the code needs to call the contains method frequently to search for data, the set List will be converted into a HashSet implementation first, and the time complexity of O(n) will be O(1).

Example:

List<Object> List = new ArrayList<>(); for (int i = 0; i <= Integer.MAX_VALUE; I ++){// Time complexity is O(n) if (list. Contains (I)) system.out.println ("list contains "+ I); }Copy the code

Is:

List<Object> List = new ArrayList<>(); Set<Object> set = new HashSet<>(); for (int i = 0; i <= Integer.MAX_VALUE; If (set.contains(I)){system.out.println ("list contains "+ I); }}Copy the code

Use static code blocks to assign static member variables

Static code block assignments should be used for static member variables of collection types, rather than collection implementations.

Example:

Private static Map<String, Integer> Map = new HashMap<String, Integer>(){{map.put("Leo",1); 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"); }};Copy the code

Is:

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"); }Copy the code

Delete unused local variables, method parameters, private methods, fields, and extra parentheses.

Mask constructors in utility classes

A utility class is a collection of static fields and functions that should not be instantiated; However, Java adds an implicit public constructor for every class that does not explicitly define a constructor. To avoid unnecessary instantiation, you should explicitly define a private constructor to mask this implicit public constructor.

Example:

Public Class PasswordUtils {// Negative example of the utility constructor private static final Logger LOG = LoggerFactory.getLogger(PasswordUtils.  public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES"; public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }Copy the code

Is:

Public Class PasswordUtils {// Public static final Logger LOG = LoggerFactory.getLogger(Passwordutils.class);  // Define a private constructor to mask the implicit public constructor private PasswordUtils(){} public static final String DEFAULT_CRYPT_ALGO = "PBEWithMD5AndDES"; public static String encryptPassword(String aPassword) throws IOException { return new PasswordUtils(aPassword).encrypt(); }Copy the code

Delete the redundant exception catch and run out

After catching an exception with a catch statement, if nothing is done and the exception is simply thrown again, the effect is the same as if the exception were not caught. You can remove this piece of code or add other handling.

Example:

Private static String fileReader(String fileName)throws IOException{try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) ! = null) { builder.append(line); } return builder.toString(); } catch (Exception e) {// throw e; }}Copy the code

Is:

Private static String fileReader(String fileName)throws IOException{try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) ! = null) { builder.append(line); } return builder.toString(); /*catch (Exception e) {return "fileReader Exception "; * /}}}Copy the code