[Java] My Coding Style Summary

Source: Internet
Author: User
Tags finally block

1. Indent

Indent with 4 spaces

2. tab

Do not use "hard" tab. You need to change the default configuration of the editor. Change the tab value to 4 spaces.

3. Line break

3.1 Line no more than 75 characters

3.2 If it is too long, it can be in:

      • "," after line break
      • Line break before operator

4. Spaces

4.1. Add a space between the keyword and (), for example: while ()

4.2. Add spaces before and after operators

4.3. Add a space after "," in the parameter list, such as: test (int ID, string name)

4.4. Cast the type with a space, such as: (object) x

5. Brackets

Do not omit curly braces and parentheses

6. Blank Line

6.1. Add a blank line after the imported package

6.2. Add a blank line between two methods

6.3. Add a blank line between the local variable and the first logical statement in the method

6.4. If, while, a for control statement before a blank line

6.5. Add blank lines before line comments

7. Naming

7.1. The most important thing is to meaningful

7.2. Do not use uncommon words

7.3. A method name that is too long may contain multiple functions that you can try to split the method

7.4. When abbreviated, it is recommended to put on the meta tape, such as Message + MSG

7.5. Hump naming method.

7.6. Constants: All capitals are required, and words are added "_" between words.

7.7. Package: Each letter in the word is lowercase for example: Java.lang

7.8. Class commands: The word must be a noun, and the first letter capitalized

7.9. Interface: The word must be a noun, and the first letter capitalized

7.10. Method: The word must be a verb, and the first letter lowercase

7.11. Collection array: The name must be plural

7.12. Unimportant, temporary variables: using standard nouns, such as Object O, Exception E, character C, D, E

8. Notes

8.1. Comments on classes and methods must be.

8.2. Remove useless comment, you can understand the meaning by the variable name

8.3. If there is a special block of code that is misleading, you need to annotate it.

8.4. If there is a multilayer loop, you can annotate the tail of the loop body, such as//end while

8.5. If you use fall through in switch, you need to annotate it, otherwise, it is usually to add a break;

9. Other

9.1. Class member variable: Need to use GET, set method read, setting

9.2. Important or repetitive expressions or methods should be extracted.

9.2. Exception handling: Do not ignore any exception information. If you want to track more advanced exceptions, do not ignore the lower level exception information.

9.3. Release resources: If you need to release resources, you need to put them in a finally block.

Other references:

Http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

http://www.infoq.com/news/2014/02/google-java-coding-standards/

[Java] My Coding Style Summary

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.