Write on the front
Because of the Java object-oriented programming features, you should choose nouns when naming. At the time of naming, the most common is the Hump nomenclature (camel-case): When a variable name or function name is a unique identifier consisting of one or more words, the first letter starts with lowercase letters, except for the first word.
I. Written specification of package name (packages)
It is recommended to prefix the package name with the top-level domain name of the company or organization to ensure that the company / The uniqueness of the package name used within the organization. The package name is all lowercase and has a practical distinction.
1.1 General Requirements
1, the choice of meaningful names, can quickly convey the purpose of the class.
2. All packages must be named with lowercase English letters.
1.2 Practical Application
Hierarchy is often applied in the application system, DAO layer (Database access), service layer (business processing), Web layer (page control action Class).
1, the package name of the first few for fixed name, if the site, the use of the site's domain name of the anti-write, if the domain name has not been determined, using the company fixed several names. such as: Net.vschool
2. The next word in the package name is the name of the module. For example: User module, package name Net.vschool.user
3, about the module access operation, the use of layered form, generally divided into:
DAO layer operations: generally defined in Net.vschool.xxx.dao, where xxx is the module name.
Service layer operations: generally defined in Net.vschool.xxx.servie.
Web layer operations: generally defined in net.vschool.xxx.action.
1.3 Examples of user modules are as follows:
Net.vschool.user
Net.vschool.user.dao
Net.vschool.user.action
Net.vschool.user.service
Writing Standard of class name (class)
The class name must use a noun, if a class name contains more than one word, then the first letter of each word uppercase, followed by lowercase letters, undulating hump-like, called the camel name. When naming a class name, you must ensure that it is accurate, concise, and easy to understand. Use full words as much as possible and avoid abbreviations (except those that are generally accepted)
2.1 General Requirements
1, the choice of meaningful names, can quickly convey the purpose of the class.
2, referring to the Java Hump naming method, the first letter of the class name must be in uppercase form, if the class name is a combination of multiple words, then the first letter of each word must use uppercase. such as: Studentanswer.java
3, when you want to distinguish between the interface class and implementation class, you can add "Impl" after the class. such as: interface class: Userinterface.java interface implementation class: Userinterfaceimpl
4, the recommended entity class does not have a suffix name.
2.2 Practical Application
Hierarchy is often applied in the application system, the DAO layer (database access), service layer (business processing), Web layer (page Control action Class), each layer of the class name as far as possible with the layer suffix.
1. DAO layer
A, interface class: In the form of Javabean+interface+dao to define, that is, the entity object +interface+dao. Such as: User Object interface class: Userinterfacedao, where xxx is the module name.
b, the implementation of the class: the form of Javabean+interface+impl+dao to define, that is, the entity object +interface+impl+dao. For example: User Object implementation class: Userinterfaceimpldao
2. Service Layer
A, the interface class: Uses the form of xxx+interface+service to define, namely, the module +interface+service. Such as: User Management interface class: Usermsginterfaceserviec
b, the implementation of the class: the form of xxx+interface+impl+service to define, that is, the module +interface+impl+service. Such as: User Management implementation class: Usermsginterfaceimplserviec
3. Web layer (Action Class)
A, the realization class: Uses the county xxx+operator+action the form to define, namely, the module + operation +action. Example: User module user+ delete operation delete+action = Userdeleteaction
Iii. Naming of methods
3.1 General Requirements
1, choose meaningful name, can quickly convey the purpose of the method.
2, referring to the Java Hump naming method, the first letter starts with lowercase, each word capitalized (except for the first word).
3.2 Practical Application
1, the method represents an action, it represents a movement, preferably a verb or a verb phrase or the first word is a verb.
2, Attribute method: Start with Get/set, followed by field name, field name first letter uppercase. such as: GetUserName ()
3, the data layer method: Only insert (insert), delete (delete), update (update), select (Find), count (statistics) at the beginning, the other layer method to avoid the beginning of this 5 words, so as not to cause misunderstanding.
4, the service layer method, according to the behavior of the method named, only describes the meaning of the method, but not the purpose of the method named. For example, the system to add new users, the user can register the front desk, can also be added to the administrator background, the method will be reused, so it is best not to use the register, using add will be better written. Avoid using methods that are related to the Web tier.
5, the Web layer method is best close to the Web language, such as Register,login,logout and other methods.
Iv. Naming of variables
4.1 Common Variables
4.1.1 General Requirements
1. By Choosing a meaningful name, you can quickly communicate the purpose of the variable.
2, referring to the Java Hump naming method, the first letter starts with lowercase, each word capitalized (except for the first word).
4.1.2 Practical Application
1, the variable name uses the basic structure as Typevariablename, uses the 3 character prefix to represent the data type.
Data type or object type |
Variable prefix |
Note |
Byte |
Bye |
1, do array time, and then prefix-A, such as String array: Astr, 2. A variable of a custom type can use its own name and change the initial letter to lowercase. 3, the use of name to be able to represent the meaning of the method. If Employee list: EmployeeList |
Char |
Chr |
Float |
Flt |
Boolean |
bln |
Integer/int |
Int |
Short |
Sht |
Long/long |
Lng |
Double/double |
Dbl |
String |
Str |
For example, define an shaping variable: intdoccount, where int indicates the data type, followed by the English name of the table, with the first letter of each word capitalized.
2, the use of variable skills:
A, in a function does not use the same variable to represent the meaning of the two values before and after.
b, unless it is in the loop, it is generally not recommended to use a single letter as the variable name, I, J, K, etc. only as a circular index variable of the small loop.
C, avoid naming state variables with flag.
D, use is to name the logical variables, such as: Blnfileisfound. This gives the Boolean variable a positive form of naming, allowing other developers to understand more clearly what Boolean variables represent.
E, if you need to abbreviate the variable name, be sure to pay attention to the consistency of the abbreviation rules throughout the code. For example, if you use intcnt in some areas of your code, and you use intcount in other areas, you add unnecessary complexity to your code. It is recommended to avoid abbreviations in variable names.
4.2 Static Variables
1, choose meaningful name, can quickly convey the purpose of the variable.
2, referring to the Java Hump naming method, the use of all uppercase form to write, for the use of multi-word synthesis of the variable using "_" to connect the words. such as: User_size
Five, the JSP page name writing specification
1. All use lowercase English characters and "_" composition.
2. The whole takes the form of module name + operation. such as: user_view.jsp
3. The JSP page corresponds to the action's meaning as much as possible, such as the userlistaction counterpart user_list.jsp
Vi. written specifications for annotations (Javadoc)
Principle: 1, annotation form uniform throughout the application, the annotations are constructed using styles with consistent punctuation and structure. 2, note content accurate concise content to be simple, clear, the meaning is accurate, prevents the annotation ambiguity, the wrong annotation not only is useless but is harmful.
1, basic note (must add) (a) class (interface) Comment (b) Comment of the constructor (c) method Comments (d) Comment on the global variable (e) field/attribute Comment Note: Simple code to make a simple comment, note content is not more than 10 words, in addition, persisted object or VO The getter and setter methods of the object need not be annotated.
2. Special required annotations (must add) (a) A typical algorithm must have comments. (b) There must be a comment in the code that is not clear. (c) Add a comment that modifies the identity at the point of code modification. (d) Annotate the code that consists of loops and logical branches. (e) The interfaces provided to others must be annotated in detail.
Comment Format:
1, single-line (single-line) Comment: "//..."
2. Block comment: "/*......*/"
3. Document Comment: "/**......*/"
4. Javadoc annotation Tag syntax
@author description of the class indicates the author of the development of such a module
@version the description of the class indicates the version of the class module
@see a reference to the description of classes, properties, methods, and related topics
Description of a parameter in a method @param description of the method
Description of the method return value @return The description of the method
@exception the description of the method describes the exceptions that the method might throw.
Method notes:
For example:
public class Test extends button {/** * adds color to the buttons * @param color of the color button * @return * @exception (method with Exception) * @author Admin Istrator * @Time 2016-11-20 15:02:29 */public voidaddcolor (String color) { ... }}
Global variable Comment: (refer to the comment string)
For example:
Public final class String implements Java.io.Serializable, comparable<string>,charsequence { /** the Valu E is used for characterstorage. */ Private final char value[]; /** The offset is the first index of thestorage in used. */ private final int offset; /** The count is the number of Charactersin the String. */ private final int count; /** Cache The hash code for the string */ private int hash;//Default to 0 ... }
Field/Attribute Comment:
For example:
public class Emailbody implements serializable{ private String ID; private string sendername;//sender name private string title;//cannot exceed 120 Chinese characters private string content;//message body private string attach;//attachment, if any, private string totalcount;//Total sender number private string successcount;//succeeded The number of people sent private Integer isdelete;//0 does not remove 1 delete private Date createtime;//currently does not support timing so when created immediately send PRIVATESET&L T emaillist>emaillist; ...... }
Java Development Naming conventions