Java Annotations and naming conventions

Source: Internet
Author: User
Tags coding standards

I. The significance of the existence of norms

The application coding specification is particularly important for the software itself and for software developers for several reasons:

    • 1, good coding standards can reduce the maintenance cost of a software as much as possible, and almost no software, in its entire life cycle, by the original developer to maintain;
    • 2, good coding standards can improve the readability of the software, allowing developers to quickly and thoroughly understand the new code;
    • 3, good coding standards can maximize the team development cooperation efficiency;
    • 4, the long-term normative code can also allow developers to develop good coding habits, and even exercise more rigorous thinking;

Second, naming norms

1. General Concept

    • 1. Use full English descriptors as far as possible
    • 2. Adoption of terminology applicable to related fields
    • 3, the use of mixed case to make the name readable
    • 4, to minimize the use of abbreviations, but if used, must conform to the entire project of the unified definition
    • 5. Avoid using long names (less than 15 letters for normal selection)
    • 6, avoid the use of similar names, or just the case of different names
    • 7. Avoid using underscores (except static constants, etc.)

2. Identifier type description

1. Naming of packages (package)
The package name should be in full English descriptors, which are made up of a lowercase word. And the prefix of the package name is always a top-level domain,
Usually com, edu, gov, mil, net, org, etc.;
such as: Com.yjhmily.test

2. Naming of classes (class)
The class name should be a noun, in a mixed case, capitalized by the first letter of each word. Try to ensure that the class name is concise and rich in description.
Use full words to avoid abbreviations (unless the project has a uniform abbreviation specification or the abbreviation is more widely used, like URLs, HTML)
such as: filedescription

3, the name of the interface (Interface)
Similar to the naming conventions of Class. On the basis of satisfying the CLASSD naming rules, ensure that the first letter of the beginning is "I",
Easy to distinguish from normal class. Its implementation class name takes the second letter of the interface name to the last, and satisfies the naming specification of the class name;
such as: Imenuengine

4. Enumeration (enum) naming
Similar to the naming conventions of Class. On the basis of satisfying the CLASSD naming rules, guarantee the first letter "E" at the beginning,
Easy to distinguish from normal class.
such as: Euserrole

5. Naming of exceptions (Exception)
Exceptions (Exception) typically use the letter E to indicate an exception, and for a custom exception class, the suffix must be Exception
such as: businessexception

6. Naming Methods (method)
The method name is a verb, in mixed case, the first letter in lowercase, followed by the first letter of the word capital.
The method name describes the action behavior of the method as much as possible. A method that returns a Boolean of type is typically preceded by "is" or "has"
such as: Getcurrentuser (), AddUser (), hasauthority ()

7, the name of the parameter (Param)
The first letter is lowercase, followed by the first letter of the word. The parameter number name does not allow an underscore or dollar sign to start,
Although this is syntactically permissible. Parameter names should be short and descriptive.
such as: Public UserContext getloginuser (String loginName);

8, the name of the constant field (Constants)
Static constants fields (static final) are all uppercase letters, and the words are separated by underscores;
such as: public static final Long FEEDBACK;
public static Long User_status;

Third, the annotation specification

A good rule of thumb to follow in relation to annotations is:

Ask yourself, if you have never seen this code, in a reasonable time to effectively understand the code, what information do you need???

1. General Concept

    • 1. Comments should increase the clarity of the code
    • 2. Keep the Annotations concise
    • 3. Write a comment before writing the code or both
    • 4. Comment out why you did something, not just what you did.

2. Note which parts

1. Java File: The copyright information and the creation time and author of the file must be stated;

2, Class: The purpose of the class, that is, the function completed by the class, and the time and author name of the class creation; When multiple people edit or modify the same class at a time,
The names of multiple persons shall appear at the author's name;

3. Interface: On the basis of satisfying class annotations, the interface annotations should include the purpose of setting up the interface, how it should be used, and how it should not be used.
The corresponding implementation class can be annotated without the explicit interface annotations;

4. Method Comment: For the method that sets (set method) and gets (get method) member, in case the member variable already has the description,
The normal member method requires a description of what is done, what the parameter means and what the value is, and how the method is created
Time must be clearly annotated to provide valuable clues for future maintenance and reading;

5. Internal annotation of the method: control structure, what the code does and why it is done, processing order, etc., especially the complex logic processing part,
To give as much detail as possible to the comments;

6, Parameters: The meaning of the parameters, and any other constraints or preconditions;

7, attribute: Description of the field;

8, local (middle) Variable: No special significance of the case without comment;

3. Comment Format

Single-line comment://comment Content

Multiline Comment:/* ... Comment content .... * /

text Comment:/** ... comment content .... */

Java Annotations and naming conventions

Related Article

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.