Service/dao Layer Method Naming conventions:
1) The method to get a single object is prefixed with get.
2) The method to get multiple objects is prefixed with the list.
3) The method that gets the statistic value is prefixed with count.
4) Insert the method prefix with save (recommended) or insert.
5) Remove the method prefix with remove (recommended) or delete.
6) The modified method is prefixed with update.
Domain Model Naming conventions:
1) Data object: Xxxdo, XXX is the data table name.
2) Data Transfer object: Xxxdto, xxx for the business domain related names.
3) Display object: Xxxvo, XXX is generally the name of the Web page.
4) POJO is a general term for DO/DTO/BO/VO, and is forbidden to be named Xxxpojo.
Constant definition:
1) do not allow any magical values (that is, undefined constants) to appear directly in the code. Counter example: String key = "Id # Taobao _" + TradeID
2) do not use a constant class to maintain all constants, should be classified according to the constant function, maintenance separately. For example: Cache-related constants are placed under Class: Cacheconsts; system configuration-related constants are placed under class: Configconsts. Description: Chatty constant class, must use the Find function to locate the modified constant, is not conducive to understanding and maintenance.
3) If the value of the variable changes only within one scope and has an extension attribute other than the name, it is defined as an enumeration class. The number in the following example is the extension information that represents the day of the week. Positive examples: public Enum {MONDAY (1), Tuesday (2), Wednesday (3), Thursday (4), FRIDAY (5), SATURDAY (6), SUNDAY (7);}
Code format:
1) There is no space between the left parenthesis and the character, and there is no space between the right parenthesis and the character. See section 5th below for a prompt. Counter Example: if (space a = = B space)
2) A space must be added between the reserved words and the brackets in the if/for/while/switch/do.
3) The left and right sides of any two-and three-mesh operators need a space . Description: Operators include assignment operators =, logical operators &&, subtraction symbols, and so on .
4) a single-line character limit of no more than 120, beyond the need to wrap, line breaks follow the following guidelines:
1) The second line indents 4 spaces relative to the first line, starting with the third row, and no longer indents, referencing the example.
2) operator wraps with the following line.
3) The point symbol of the method call wraps with the following line.
4) in more than one parameter long, after the comma wrap.
5) do not wrap before the brackets, see counter example.
Positive example:
StringBuffer sb = new StringBuffer ();
With more than 120 characters, line breaks indent 4 spaces, and the point symbol before the method wraps
Sb.append ("Zi"). Append ("Xin") ...
. Append ("Huang") ...
. Append ("Huang") ...
. Append ("Huang");
5) When the method parameter is defined and passed in, a space must be added to the comma behind the multiple parameters. In the following example, "a" in the argument below must have a space behind it. Method ("A", "B", "C");
OOP constraints:
1) Avoid using the object reference of a class to access static or static methods of this class, needlessly increase the cost of compiler parsing, directly with the class name to access.
2) All overwrite methods must be added @ Override annotations. Description: GetObject () issue with get 0 bject (). One is the letter O, one is the number of 0, plus @ Override can accurately determine whether the overwrite success. In addition, if the method signature is modified in an abstract class, the implementation class compiles the error immediately.
3) The same parameter type, the same business meaning, you can use the variable parameters of Java, avoid using Object. Description: The mutable parameter must be placed at the end of the parameter list. (Encourage students to try to use variable parameter programming) Example: Public User getusers (String type, Integer ... ids) {...}
4) The Equals method of Object is apt to throw a null pointer exception, and you should call equals using a constant or a value-determining object. Positive example: "Test". Equals (object), counter example: Object.Equals ("test"); Description: Java is recommended. Util. Objects # Equals (tool class introduced in JDK 7)
5) The standard of use for basic data types and wrapper data types is as follows: "Force" all POJO class properties must use the wrapper data type. The return value and parameters of the "force" RPC method must use the wrapper data type. "Recommended" all local variables use the base data type.
6) "Recommended" loop body, the connection of strings, using the StringBuilder Append method to expand.
Description: The deserialized bytecode file shows that each loop will be new to a StringBuilder object and then
The Append operation finally returns a String object through the ToString method, resulting in a waste of memory resources.
Counter Example:
String str = "Start";
for (int i = 0; i <; i++) {
str = str + "Hello";
}
Collection Processing:
1. "Enforce" the handling of hashcode and equals, following the following rules: 1) As long as you rewrite equals, you must rewrite hashcode.
2) Because set stores objects that are not duplicates and are judged according to Hashcode and equals, the set stored objects must override both methods.
3) If the custom object is a key to Map, you must override Hashcode and equals. Description: String overrides the Hashcode and equals methods, so we can use the string object as a key very happily.
2. The sublist result of the "force" ArrayList cannot be strongly turned into ArrayList, otherwise the classcastexception exception will be thrown: java. Util. Randomaccesssublist cannot is cast to Java. Util. ArrayList;
Description: Sublist returned the inner class sublist of ArrayList, not ArrayList, but a view of ArrayList, and all operations on the Sublist sub-list would eventually be reflected on the original list.
3. "Forced" in the sublist scene, a high degree of attention to the number of elements of the original set of changes, will cause the child list traversal, increment, delete all produce concurrentmodificationexception exception.
4. "Force" the use of the collection to the array of methods, must use the collection of ToArray (t[] array), passed in an array of exactly the same type, size is list. Size (). Note: Using the ToArray parameter method, when the array space of the incoming parameter assignment is not large enough, the ToArray method will be reassigned internally
Memory space, and returns the new array address, if the array element is larger than is actually required, the array element labeled [list. Size ()] is set to null, and the other array elements retain the original value, so it is best to align the method into the parameter group size definition with the number of collection elements.
Positive example:
list<string> list = new arraylist<string> (2);
List.add ("Guan");
List.add ("Bao");
string[] Array = new string[list.size ()];
Array = List.toarray (array);
Inverse example: the direct use of the ToArray method has a problem, this method return value can only be object[] class, if strong to other types of array will appear classcastexception error.
5. "Force" using the tool class Arrays. Aslist () When you convert an array to a collection, you cannot use it to modify a collection-related method, and its Add/remove/clear method throws a Unsupportedoperationexception exception. Description: The returned object of Aslist is a Arrays inner class and does not implement a modified method of the collection. Arrays. Aslist
The embodiment is the adapter mode, only the conversion interface, the background data is still an array.
string[] str = new string[] {"A", "B"};
List List = Arrays.aslist (str);
First case: List.add ("C"); Run-time exception.
Second case: str[0] = "Gujin"; Then the List.get (0) will be modified as well.
6. "Mandatory" generic wildcard character <? Extends T > To receive the returned data, the generic collection of this notation cannot use the Add method, and < Super T> cannot use the Get method and is error-prone when assigning values to an interface call. Description: Extension say PECS (Producer Extends Consumer Super) principle: 1) Read content frequently
, which is suitable for Extends with upper bounds. 2) often inserted into the, suitable for the Nether Super.
7. "Force" do not perform the remove/add operation of elements in the Foreach loop. The remove element uses the Iterator method, which requires a lock on the Iterator object if the concurrency operation.
Positive example:
Iterator<string> it = A.iterator ();
while (It.hasnext ()) {
String temp = It.next ();
If (delete element's condition) {
It.remove ();
}
}
Counter Example:
List<string> a = new arraylist<string> ();
A.add ("1");
A.add ("2");
for (String temp:a) {
if ("1". Equals (temp)) {
A.remove (temp);
}
}
Note: The above code implementation results will certainly surprise everyone, then try to replace "1" with "2", will be the same result?
8. "Mandatory" in JDK 7 version and above, Comparator to meet the following three conditions, otherwise Arrays. Sort, collections. Sort will report illegalargumentexception exceptions.
Description
1) The comparison of X, Y and y, X, results in the opposite direction.
2) x > y, y > Z, then x > Z.
3) x = y, then x, z compare result and Y, Z compare result is same.
Counter example: In the following example, there is no case of equality, and an exception may occur in actual use:
New Comparator<student> () {
@Override
public int Compare (Student O1, Student O2) {
return O1.getid () > O2.getid ()? 1:-1;
}
};
9. When the "recommended" collection is initialized, specify the initial value size of the collection. Description: HashMap is initialized with HashMap (int initialcapacity), positive example: Initialcapacity = (number of elements required to be stored/load factor) + 1. Note the load factor (that is, loader
Factor) defaults to 0.75, set to 16 if the initial value size is temporarily not determined. Counter example: HashMap need to place 1024 elements, because the initial size of the capacity is not set, as the elements continue to increase, capacity 7 times forced to expand, resize need to rebuild the hash table, severely affect performance.
10. "Recommended" use EntrySet to traverse the Map class set KV instead of KeySet way. Description: KeySet is actually traversed 2 times, one is to Iterator object, and the other is to remove the key from the hashMap corresponding value. And EntrySet just traversed once to put key and value in the entry, the effect
Higher rates. If it is JDK 8, use Map. The Foreach method. A positive example: values () returns a set of V values, a list collection object, KeySet () returns a set of K values, a set collection object, and EntrySet () returns a collection of K-v value combinations.
11. "Recommended" Height note the Map class collection k/v can not store null values, the following table: Collection Class Collection Class Key Value Super Description Description
Hashtable does not allow null Dictionary thread safety
Concurrenthashmap does not allow NULL to disallow NULL ABSTRACTMAP segmented lock technology
TREEMAP disallow null for NULL ABSTRACTMAP thread unsafe
HashMap allow NULL to allow null ABSTRACTMAP thread unsafe
Counter example: Due to HashMap interference, many people think that concurrenthashmap is possible to place null values, whereas in fact, a NPE exception is thrown when storing null values.
12. "References" make good use of the order of the set (sort) and stability (order) to avoid the negative effects of the disorder of the set (Unsort) and instability (Unorder). Description: Ordering is the result of a traversal that is arranged in sequence according to a certain comparison rule. Stability refers to the elements of each iteration of the set
The order is certain. such as: ArrayList is Order/unsort;hashmap is Unorder/unsort;treeset is order/sort.
13. "Reference" using the unique features of the set element, you can quickly redo a collection, avoiding the use of the Contains method of List to traverse, contrast, and redo operations.
Java Development Specification Summary