J? A? V? A developer? Name? Rules? Fan

Source: Internet
Author: User

The purpose of the definition specification is to unify the code style of the project and make the program readable.

Package name (all lowercase, defined by domain name)

The Java package name is composed of lowercase words. However, due to the characteristics of Java object-oriented programming, every Java programmer can write their own Java packages. To ensure the uniqueness of each Java package name, in the latest Java programming specification, the programmer is required to add a unique prefix before the name of the defined package. Because domain names on the Internet are not repeated, programmers generally use their own domain names on the Internet as the unique prefix of their packages.
Example: net. frontfree. javagroup


Class Name (uppercase)

According to the Conventions, Java class names often start with uppercase letters. If the class name is composed of multiple words, the first letter of each word should be a large one, for example, testpage. If the class name contains the abbreviation, each letter of the word should be capitalized, for example, xmlexample. Another naming technique is that classes are designed to represent objects. Therefore, you should select nouns when naming classes.
Example: Graphics


Method Name (lowercase letters, uppercase letters)

The first word of the method name should start with a lower-case letter, and the subsequent words should start with an upper-case letter.
Example: drawimage


Constant name (all uppercase, often underlined)
The constant names should all use uppercase letters and indicate the complete meaning of the constant. If a constant name is composed of multiple words, the words should be separated by underscores.
Example: max_value


Parameter Name

The parameter naming rules are the same as the method naming rules. To avoid confusion during reading the program, make sure that the parameter name is a word as clear as possible.

Javadoc comments

In addition to using our common annotation methods, the Java language specification also defines a special annotation, that is, the javadoc annotation we call, it is used to record APIs in our code. Javadoc comments are multiline comments that start with/** and end with */. Comments can contain HTML tags and specialized keywords. The advantage of using javadoc annotations is that the compiled notes can be automatically converted into online documents, saving the trouble of writing program documents separately.
For example:

/**
* This is an example
* Javadoc
* @ Author darchon
* @ Version 0.1, 10/11/2002
*/

At the beginning of each program, javadoc is generally used to comment out the overall description of the program and copyright information. Then, javadoc comments can be added to each class, interface, method, and field in the main program, the first part of each comment first summarizes the functions completed by the class, interface, method, and field in one sentence. This sentence should take a separate line to highlight its generalization, after this sentence, you can follow more detailed descriptions. After descriptive paragraphs, you can also follow special paragraphs starting with the javadoc annotation label, such as @ auther and @ version in the preceding example. These paragraphs are displayed in a specific way in the generated documents.


Variable and constant naming


Variable name

There are three main naming rules:
Camel markup: the first letter is lowercase, and the subsequent words start with an uppercase letter.
Pascal markup: the first letter is in upper case, and the subsequent words start with an upper letter.
Hungary markup: The variable type is described by appending a lower-case sequence before the variable in Pascal notation.

In Java, we generally use the Hungary markup method. The basic structure is scope_typevariablename. It uses a 1-3 character prefix to indicate the data type. The three character prefix must be in lower case, the prefix is followed by a name consisting of one or more words with strong ideographic meaning. The first letter of each word is uppercase, and the other is lowercase, this ensures that the variable name can be correctly broken. For example, define an integer variable to record the document quantity: intdoccount, where int indicates the data type, followed by the meaning of the English name, each word is capitalized. In this way, a variable name can reflect the meaning of the variable type and the value stored in the variable, which makes the code statement readable and easier to understand. Byte, Int, Char, long, float, double, Boolean, and short.

Data Type/prefix (Appendix)
Byte B
Char C
Short sh
Int I
Long L
Char C
String s
Float F
Double D
Hashtable H
[] Arr
List lst
Vector v
Stringbuffer sb
Boolean B
Byte BT
Map
Object ob

Add "g _" before global variables used in multiple functions _". For example, a global string variable: g_struserinfo.

Pay attention to the following points when naming variables:

· Select a meaningful name. Note that the first letter of each word must be capitalized.

· In a function, the same variable is not used to represent two values with different meanings.

· I, J, K and so on are used only as small cycle index variables.

· Do not use flag to name state variables.

· Use is to name logical variables, such as blnfileisfound. By naming Boolean variables in the affirmative form, other developers can better understand the meaning of Boolean variables.

· If necessary, append the computing qualifier at the end of the variable, for example, cursalessum.

· The name does not contain cursales and cursalessum.

· The names of static final variables (constants) should be in uppercase and indicate the complete meaning.

· If You Need To abbreviation a variable name, pay attention to the consistency of the abbreviation rules in the entire code. For example, if intcnt is used in some areas of the code and intcount is used in other areas, unnecessary complexity is added to the Code. We recommend that you do not use abbreviations in variable names.

· By placing a quantizer at the end, you can create more unified variables, which are easier to understand and search. For example, use strcustomerfirst and strcustomerlast instead of strfirstcustomer and strlastcustomer. Commonly used quantifiers include: First (the first in a group of variables), last (the last in a group of variables), and next (the next variable in a group of variables), Prev (the previous one in a group of variables), and cur (the current variable in a group of variables ).

· Select the best data type for each variable, which can reduce the memory demand, speed up code execution, and reduce the possibility of errors. The data type used for variable may affect the calculation result of the variable. In this case, the compiler does not generate runtime errors, but only forces the value to comply with the data type requirements. This type of problem is extremely difficult to find.

· Minimize the scope of variables. If the scope of a variable is greater than its expected range, the variable can continue to exist and still occupy resources for a long time after the variable 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. Resource occupation is an important issue in the scope. For variables, minimizing the scope will have a huge impact on application reliability.

For the constant naming method,

In Java code, whenever necessary, it is recommended that constants replace numbers and fixed strings. That is to say, the program should not contain any number except 0, 1. Constants can be concentrated in some definitions at the beginning of the program or in a wider scope. The names should all use uppercase letters and indicate that the constants are complete and inclusive. If a constant name is composed of multiple words, use the underscore "_" to separate these words, such as num_days_in_week and max_value.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.