Java Coding Standards

name

Class names use UpperCamelCase style. Domain model related naming: DO / DTO / VO / DAO, etc.

Method names, parameter names, member variables, and local variables all use the lowerCamelCase style.

Constant names are all uppercase and lowercase, and the words are separated by underscores. Don't think the names are too long.

Abstract classes start with Abstract or Base.

Exception class names end with Exception.

The test class name starts with the name of the class it is testing and ends with Test.

Remember, square brackets are part of the array type, String[] args, don't use String args[].

Do not add is to any boolean variable in the POJO class, otherwise some frameworks will cause serialization errors.

Enumeration class names are recommended to be suffixed with Enum. Enumeration member names need to be all capitalized, and words should be separated by underscores.

Service/DAO layer naming convention

  1. Get a single object prefixed with get.
  2. Get multiple objects prefixed with list.
  3. Methods for getting statistics are prefixed with count.
  4. Insert method with save.
  5. The delete method uses remove.
  6. The modification method is to use update.

format specification

  1. Do not break a line before the opening parenthesis.
  2. Newline after opening brace.
  3. Newline before closing brace.
  4. Codes such as else after the curly braces do not wrap; it means that the terminating closing curly brace must be wrapped.

Collection processing

  1. To use the method of converting a collection to an array, you must use the toArray(T[] array) of the collection, and the incoming array is of the same type, and the size is list.size().
  2. Use the utility class Array.asList() to convert an array into a collection.
  3. Do not perform remove/add operations on elements in a foreach loop. The remove element uses the Iterator method. If the operation is performed concurrently, the Iterator object needs to be locked.

Iteator<String> it = a.iterator(); while(it.hasNext()){ String temp = it.next(); if(条件){ it.remove(); } }

  1. Set initialization, try to specify the set initialization size.
  2. Use entrySet to traverse the Map set KV instead of the keySet method (the keySet method is traversed twice, one is converted to an Iterator object, and the other is to retrieve the value corresponding to the key from the hashMap, JDK8, using the Map.foreach method).

Thread Pool

  1. Thread pools are not allowed to be created by Executors, but through ThreadPoolExecutor. This way of handling allows the writers to clarify the running rules of the thread pool and avoid risks as much as possible.
  2. Use CountDownLatch for asynchronous to synchronous operation. Before each thread exits, the countDown method must be called. The thread executes the cache exception to ensure that the countDown method can be executed.
  3. Avoid the Random instance being used by multiple threads. Although sharing the instance is thread-safe, it will cause performance degradation due to competition for the same seed.
  4. The statements in the loop body should consider performance. The following operations should be moved to the outside of the loop as much as possible, such as defining objects, variables, obtaining database connections, and performing unnecessary try-catch operations.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325022334&siteId=291194637