Self-organizing a set of Java coding rules, so that you write code is not so messy

Source: Internet
Author: User

1. Naming conventions

(1) All identifiers can only be Ascⅱ letters (A-Z or a-Z), Numbers (0-9), and underscores "_".

(2) The class name is a noun, in the form of a mixed case, capitalized by the first letter of each word. Example: UserInfo

(3) The casing rules of the interface are similar to the class name: for example: UserInterface.

(4) The method name is a verb or a verb phrase, in the form of a mixed case, the first letter is lowercase, and then the first letter of the word is capitalized. For example: SetName (String name).

(5) The name of the variable is camel-named, the first letter is lowercase, and the first letter of any intermediate word is capitalized. Variable names should be short and as the name implies, easy to remember. The member variables of the class (except for the automatically generated mapper class, all starting with lowercase m).

(6) The declaration of a constant should be capitalized, and each word is connected with "_". For example: final String FTP = "FTP://192.168.1.2";

2. Annotation specification

(1) Note use "//" as much as possible, and "/*" for all Javadoc comments;

(2) All source files have a comment at the beginning, listing the author and time, using the MyEclipse template.

(3) Each method must add a comment (except the main () method of the Class).

(4) Each attribute must have a comment added.

(5) The code contains at least 15% comments. The JS code should be more than 30% comments.

(6) Use Chinese as a note.

3. Indentation and typesetting specifications

(1) Avoid a line that is longer than 60 characters.

(2) Use the formatting capabilities of Eclipse's source code to complete indentation of the code.

4. File name specification

(1) A Java source file can only store one Java class.

(2) The file name is the same as the Java class name.

(3) The line of code for a class file does not exceed 200 lines.

5. Declaration specification

(1) A row declares a variable.

(2) Do not put declarations of different types of variables on the same line.

(3) Declare the variable only at the beginning of the code block.

(4) All variables must be initialized at the time of declaration.

(5) Avoid declaring a local variable that overrides a variable declared at the previous level.

(6) The method and method are separated by a blank line.

6. Statement specification

(1) Each line contains at least one simple statement.

(2) In the return statement, the return value is not enclosed in parentheses "()".

(3) If statements are always enclosed in "{" and "}". Each "{" or "}" occupies a single row.

(4) In the initialization or UPDATE clause of the FOR statement, avoid using more than three variables, resulting in a higher degree of complexity.

(5) When a case of switch is executed down (because there is no break statement), a comment should normally be added to the position of the break statement.

7. Programming Specifications

(1) Provide public or private access control of the instance and class variables, with or without the default value or protected access control.

(2) Avoid accessing static variables or methods of a class with an object. The class name should be used instead.

(3) Avoid assigning the same value to multiple variables in a single statement.

(4) Use the switch () statement to implement the multi-channel branch.

(5) If you are using JDBC, consider using java.sql.PreparedStatement instead of java.sql.Statement.

(6) The method prefix used to set the state of the object must be set; the method prefix used to retrieve the state of a Boolean type object must be is, and the other method prefix must be get.

(7) The program should use as few numbers (or characters) as possible, define static variables to illustrate the meaning of the number (or character). When you need to assign or compare in a program, use the static variables defined earlier. Exceptions are in loop control.

Self-organizing a set of Java coding rules, so that you write code is not so messy

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.