Java Code Specification notes (bottom)

Source: Internet
Author: User

Statement

17, recommend a one-line statement. Because it helps to write the gaze.

Other words. In the following declaration methods,

int level; Indentation Levelint size; Size of table
To be superior to:

int level, size;
Never put the declaration of a variable and a method on the same line, for example:
Long dbaddr, getdbaddr (); wrong!
Do not put declarations of different types of variables on the same line, for example:

int foo, fooarray[]; wrong!
Note: In the example above. Only one space is used between the variable and the identifier. Another way to be accepted is to use tabs, such as:

int level;//indentation levelint size;//size of Tableobject currententry; Currently selected table entry

18. Simply place the declaration at the beginning of the code block.

(a block of code refers to the code that is included between "{" and "}".) Don't wait for the first time they're used to declare variables. This will mean that the program is not noticed and the ape is confused, and at the same time it interferes with the portability of the code within that scope.

void MyMethod () {    int int1;//Beginning of method block    if (condition) {        int int2;//beginning of "if" block        ...    }}

The only exception to this rule is the index variable in the for loop, which can be declared inside a for statement in Java code.
for (int i = 0; i < maxloops; i++) {...

19. Avoid declaring a local variable that overrides the variable declared at the previous level.

For example, do not declare the same variable name in an internal code block.

int Count;...func () {    if (condition) {        int count;//should be avoided!        ...    }    ...}


20, try to initialize at the same time that the local variables are declared. The only reason for not doing this is that the initial value of the variable depends first on some calculation that occurs.

21. When writing classes and interfaces, the following formatting rules should be followed:

    • Do not have spaces between the method name and the opening parenthesis "(" of the list)
    • The left curly brace "{" is at the end of the same line of the declaration statement
    • The closing curly brace "}" is aligned with the corresponding declaration statement by another line. The "}" should be immediately after "{" Unless it is an empty statement.
Class Sample extends Object {    int ivar1;    int ivar2;    Sample (int i, int j) {        ivar1 = i;        Ivar2 = j;    }    int Emptymethod () {}    ...}

    • Separated by a blank line between methods

Statement
22. Each line includes a maximum of one statement.

Such as:

argv++; argc--; should be avoided!

23, unless there are obvious reasons, do not use commas to combine multiple statements. Such as:
if (err) {    format.print (System.out, "error"), exit (1);//Critical Error <span style= "Font-family:arial, Helvetica, Sans-serif; " >!</span>}


Java Code Specification notes (bottom)

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.