Naming specification for Java project development (organized by power node Java College)

It's better to use English instead of Chinese pinyin

1: Package: used to classify the classes that complete different functions and put them in different directories (packages). Package naming rules: reverse the company domain name as the package name. For example, www.bjpowernode.com Com for package names: each letter needs to be lowercase. For example: com bjpowernode. test; The full name of the test class under the package is: com bjpowernode. Test. java 。 If the package is not used when defining a class, Java will assume that the class we define is in the default package.

2: Class: capitalize the first letter. If a class is composed of multiple words, the first letter of each word is capitalized, and no connector is used in the middle. Try to use English. Such as connectionfactory

3: Method: the first word is all lowercase. If a method consists of multiple words, the first letter is capitalized from the second word without connectors. addPerson

4: Property: same as method. Such as ageofperson

5: Constant: all words are capitalized. If there are multiple words, use underline links.

For example:

Supplement: pay attention to the following points when naming variables:

・ choose meaningful names and note that the first letter of each word should be capitalized.

・ the same variable is not used in a function to represent two values with different meanings.

・ I, J, K, etc. are only used as loop index variables for small loops.

・ avoid naming state variables with flags.

・ 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.

・ if necessary, add a calculation qualifier at the end of the variable, such as cursalessum.

・ naming does not include cursales and cursalessum.

・ the names of static final variables (constants) should be capitalized and indicate the full meaning.

・ 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.

・ by placing a quantifier at the end, you can create more unified variables, which are easier to understand and easier to search. For example, use strcustomerfirst and strcustomerlast instead of strfirstcustomer and strlastcustomer. Common quantifier suffixes include: first (the first variable in a group), last (the last variable in a group), next (the next variable in a group), prev (the previous variable in a group), cur (the current variable in a group).

・ select the best data type for each variable, which can not only reduce the demand for memory, speed up the execution of code, but also reduce the possibility of error. The data type used for a variable may affect the results of the calculation of the variable. In this case, the compiler does not generate a run-time error, it just forces the value to meet the requirements of the data type. Such problems are extremely difficult to find.

・ minimize the scope of variables. If the scope of a variable is larger than it should be, the variable can continue to exist and still occupy resources for a long time after it is no longer needed. Their main problem is that any method in any class can modify them, and it is difficult to track where they are modified. Occupying resources is an important issue involved in scope. For variables, minimizing the scope will have a huge impact on the reliability of the application.

On the naming method of constants, whenever in Java code, constant is advocated to replace numbers and fixed strings. In other words, there should be no other numbers in the program except 0 and 1. Constants can be concentrated in the definition at the beginning of the program or in a wider scope. Names should all use uppercase letters and indicate the full meaning of the constant. If a constant name consists of more than one word, it should be underlined "" To segment these words, such as num_ DAYS_ IN_ WEEK、MAX_ VALUE。

The above is the Java project development naming specification (power node Java College) introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>