Java development naming specification summary
Precautions before use: 1. Due to the characteristics of java object-oriented programming, nouns should be selected as much as possible when naming
2. Camel case: when a variable name or function name is a unique identifier composed of one or more words, the first letter starts with lowercase and the first letter of each word is capitalized (except the first word).
For example: myfirstname
A package name writing specification (package) recommends that the top-level domain name of the company or organization be used as the prefix of the package name to ensure the uniqueness of the package name used in each company / organization. All package names are lowercase letters and have practical distinguishing significance.
1.1 General requirements 1. Select meaningful names to quickly convey the purpose of this class.
2. All packages must be named in lowercase English letters.
1.2 layers are often applied in practical application systems, including Dao layer (database access), service layer (business processing) and web layer (page control action class).
1. The first few package names are fixed names. If it is a website, the reverse writing of the website's domain name is adopted. If the domain name has not been determined, the company's fixed names are adopted. For example: net vschool
2. The next word in the package name is the name of the module. For example: user module, the package name is net vschool. user
3. The access operation of the module adopts a hierarchical form, which is generally divided into:
Dao layer operation: generally defined in net vschool. xxx. Dao, where XXX is the module name.
Service layer operation: generally defined in net vschool. xxx. In the servie.
Web layer operation: generally defined in net vschool. xxx. Action.
The following is an example of a user module:
net. vschool. user
net. vschool. user. dao
net. vschool. user. action
net. vschool. user. service
Class names must use nouns. If a class name contains multiple words, the first letter of each word is capitalized, and the subsequent letters are lowercase, undulating in a hump shape, which is named as a hump. When naming a class name, it must be accurate, concise and easy to understand. Try to use complete words and avoid abbreviations (except for recognized ones)
2.1 naming of classes 1. General requirements 1. Select meaningful names to quickly convey the purpose of this class.
2. Referring to the Java hump naming method, the first letter of the class name must be capitalized. If the class name is composed of multiple words, the first letter of each word must be capitalized. For example: studentanswer java
3. When you want to distinguish the interface class from the implementation class, you can add "impl" after the class.
For example: interface class: userinterface Java interface implementation class: userinterfaceimp
4. The recommended entity class does not have a suffix.
2.1. 2 layers are often used in practical application systems, such as Dao layer (database access), service layer (business processing) and web layer (page control action class). The class name of each layer shall be suffixed with this layer as much as possible.
1. Dao layer
a. Interface class: it is defined in the form of JavaBean + interface + Dao, that is, entity object + interface + Dao.
For example, user object interface class: userinterfacedao, where XXX is the module name.
b. Implementation class: it is defined in the form of JavaBean + interface + impl + Dao, that is, entity object
+Interface+Impl+Dao。 For example, user object implementation class: userinterfaceimpldao
2. Service layer
a. Interface class: it is defined in the form of XXX + interface + service, that is, module + interface + service.
For example, user management interface class: usermsginterfaceservice
b. Implementation class: it is defined in the form of XXX + interface + impl + service, that is, module + interface+
Impl+Service。 For example, user management implementation class: usermsginterfaceimplservic
3. Web layer (action class)
a. Implementation class: it is defined in the form of XXX + operator + action, that is, module + operation + action. as
User module user + delete + action = userdeleteaction
2.1 naming of variables 1 general variable 2.2 2.1 General requirements 1. Select a meaningful name to quickly convey the purpose of the variable.
2. Referring to the Java hump naming method, the first letter starts with lowercase, and the first letter of each word is capitalized (except the first word).
2.2. 2.2 practical application 1. The basic structure of variable naming is typevariablename, and the 3-character prefix is used to represent the data type.
For example, define an integer variable: intdoccount, where int indicates the data type, followed by an ideographic English name, and the first letter of each word is capitalized.
2. Variable usage skills:
a. In a function, the same variable is not used to represent two values with different meanings.
b. Unless it is in a loop, it is generally not recommended to use a single letter as the variable name, and I, J, K, etc. are only used as the loop index variables of small loops.
c. Avoid naming state variables with flags.
d. Use is to name logical variables, such as blnfileisfound. Through this naming method of giving positive form to Boolean variables, other developers can more clearly understand the meaning represented by Boolean variables.
e. If you need to abbreviate variable names, you must pay attention to the consistency of abbreviation rules in the whole code. For example, if intcnt is used in some areas of code and intcount is used in others, it will add unnecessary complexity to the code. It is recommended to avoid abbreviations in variable names.
2.2. 2. Static variable 1. Selecting a meaningful name can quickly convey the purpose of the variable.
2. Referring to the Java hump naming method, it is written in all uppercase, and "" is used for variables synthesized by multiple words To connect the words. E.g. user_ LIST
2.3 naming of methods 1 General requirements 1. Select a meaningful name to quickly convey the purpose of the method.
2. Referring to the Java hump naming method, the first letter starts with lowercase, and the first letter of each word is capitalized (except the first word).
2.3. 2 practical application 1. Method represents a behavior, which represents an action, preferably a verb or verb phrase, or the first word is a verb.
2. Property method: start with get / set, followed by the field name, which is capitalized. For example: getusername()
3. Data layer method: it can only start with insert, delete, update, select and count. Other layer methods should avoid starting with these five words to avoid misunderstanding.
4. The service layer method is named according to the behavior of the method. It only describes the meaning of the method, not the purpose of the method. For example, when adding a new user to the system, the user can register in the foreground or the administrator can add in the background. The method will be reused, so it is better not to use register, and it will be better to use add. Avoid methods related to the web tier.
5. The web layer method is preferably a language close to the web, such as register, login, logout and so on.
Three annotation writing specification (Javadoc) Java can adopt our common annotation methods (/ /, / * * /), the Java language specification also defines a special annotation, that is, the Javadoc annotation, which starts with / * * and ends with * /. The Javadoc annotation can be automatically converted into an online document, eliminating the trouble of writing program documents separately. It is recommended.
Javadoc annotations mainly cover classes, properties, and methods.
For example: