Java coding specification

  • Constant name all uppercase, words separated by underscores, and strive to semantic expression complete and clear, not too long name. Positive example: MAX_STOCK_COUNT Negative example: MAX_COUNT

  • Abstract class names begin with Abstract or Base; Exception class names end with Exception; A Test class name starts with the name of the class it is testing and ends with Test

  • POJO class Boolean variables, do not prefix is, otherwise part of the framework parsing will cause serialization errors. Counterexample: the attribute defined as the basic data type Boolean isDeleted, and its method is isDeleted(). During the reverse lookup, the RPC framework mistakenly thinks the corresponding attribute name isDeleted. As a result, the attribute cannot be obtained and an exception is thrown

  • Do not add any modifiers to the methods and properties in the interface classes (nor public), keep the code simple, and add valid Javadoc comments. Try not to define variables in the interface; if you do define variables, they must be related to interface methods and are the base constants for the entire application

  • For Service and DAO classes, based on the SOA philosophy, the exposed Service must be the interface, and the internal implementation class is distinguished from the interface by the Impl suffix. Example: CacheServiceImpl Implements the CacheService interface

  • If the interface name describes the capability, use the corresponding adjective as the interface name (usually in the form of – able). Example: AbstractTranslator implements the Translatable interface

  • When assigning a value to long or long, use an uppercase L after the value, not a lower case L, which can be confused with the number 1

  • The criteria for using basic data types and wrapped data types are as follows: 1) all POJO class attributes must use wrapped data types. The return values and parameters of RPC methods must use wrapper data types. 3) [Recommendation] Use basic data types for all local variables. Note: The absence of initial values for POJO class attributes reminds users that they must explicitly assign values when they need to use them. Any NPE problems, or entry checks, are guaranteed by the user. Example: The query result of the database may be null because of automatic unpacking and NPE risk of receiving with basic data type. Counterexample: for example, the rise and fall of total transaction volume is displayed, that is, plus or minus X %. X is the basic data type, and the RPC service is called. When the call is unsuccessful, the default value is returned and the page displays 0%, which is unreasonable and should be displayed as a dash. So the null value of the wrapper data type can indicate additional information, such as: remote call failure, abnormal exit

  • Methods within a class are defined in the order public or protected methods > private methods > getter/setter methods

  • Final can declare classes, member variables, methods, and local variables. The final keyword is used when: 1) classes that are not allowed to be inherited, such as the String class. 2) The referenced domain object is not allowed to be modified. 3) Methods that are not allowed to be overridden, such as setter methods of POJO classes. 4) Local variables that are reassigned during runtime are not allowed. 5) Avoid the repeated use of a variable in context, and use final descriptions to force a variable to be redefined for better refactoring

  • Volatile solves the problem of multithreaded memory not being visible. Write multiple reads can solve the variable synchronization problem, but if you write multiple, also can not solve the thread safety problem (simultaneous write problem)

  • ThreadLocal does not solve the problem of updating shared objects. Static decoration is recommended for ThreadLocal objects

  • Parameter verification is required in the following situations: 1) Methods with low call frequency. 2) Execution time consuming methods. 3) Methods that require extremely high stability and availability. 4) Open interface provided externally, no matter RPC/API/HTTP interface. 5) Sensitive access.

  • Parameter verification is not required in the following situations: 1) methods that are most likely to be called through a loop. However, external parameter inspection requirements must be noted in the method description. 2) Methods with high frequency of low-level invocation. After all, as in the final filtration of pure water, parameter errors are unlikely to be exposed at the bottom. Generally, the DAO layer and the Service layer are deployed in the same application and server, so the verification of DAO parameters can be omitted. 3) a method that is declared private will only be called by its own code. If you can be sure that the parameters passed by the code calling the method have been checked or there is no problem, you can not check the parameters.

  • NPE prevention is a basic training for programmers. Pay attention to the following scenarios: 1) If the return type is basic data type, an NPE may be generated when an object of data type is unpacked automatically. 2) The database query result may be null. 3) Even if the set is isNotEmpty, the fetched data element may be null. 4) When a remote call returns an object, null pointer judgment is required to prevent NPE. 5) For data obtained in Session, IT is recommended that NPE check to avoid null Pointers. 6) Cascade call obj.geta ().getb ().getc (); A series of calls, easy to generate NPE.

  • To the JVM environment parameter Settings – XX: + HeapDumpOnOutOfMemoryError parameters, letting the JVM dump letter touched the OOM scene output

  • In an online production environment, the JVM’s Xms and Xmx are set to the same size of memory to avoid the stress of resizing the heap after GC

mysql

  • Primary key index name pk_ field name; Unique index name uk_ field name; The common index name is the idX_ field name

  • Decimal type is decimal, and float and double are prohibited

  • The table must have three fields: ID, gmT_CREATE, gmT_MODIFIED Description: ID must be the primary key, the type must be the primary key, and the type must be Bigint unsigned. The self-increment step for a single table is, and the self-increment step for a single table is 1. Gmt_create and gmt_modified are datetime types. The former present tense indicates that the past participle is typed after being actively created, and the former present tense indicates that the past participle is passively updated after being actively created

  • You are advised to divide databases and tables only when the number of rows in a single table exceeds 5 million or the capacity of a single table exceeds 2GB

  • Prevent index invalidation due to implicit conversion caused by different field types

  • Do not use count(column name) or count(constant) instead of count(*)

  • If the in operation can be avoided, it can be avoided. If it really cannot be avoided, it is necessary to carefully evaluate the number of set elements following in and control it within 1000

  • In table query, do not use * as the list of query fields, which fields must be clearly stated

  • POJO classes cannot add IS to Boolean attributes, whereas database fields must be added is_, requiring mapping between fields and attributes in a resultMap

  • Do not use functions on columns as this will cause index invalidation and a full table scan

  • Use in where clauses should be avoided! = or not in or <> operators, because all of these operators invalidate the index and perform a full table scan

  • The use of OR to join conditions in the WHERE clause should be avoided as this can lead to index invalidation and a full table scan

  • Compound indexes comply with the left-most prefix principle, that is, the index is used only when the first field of the compound index is used in the query condition

  • Indexes do not contain columns with NULL values, so try not to default to NULL values unless there is a very specific reason to use NULL values during database design

  • When the types on the left and right sides of the query condition do not match, implicit conversion will occur. The influence of implicit conversion is that it may cause index failure and carry out full table scan