Refer to Sun's Java coding specification, semi-translation semi-comprehension.
- Use blank lines to split blocks of code and add comments to code blocks;
- A file a class (or interface) that places a private class behind a common class that references that class;
- Source file composition: The declaration of a file comment, Package/import, class, or interface;
-
/* * Class Name * Version Info * Copyright Notice * Program Description */ |
- Use 4 spaces as indentation, avoid using the TAB key (Tab key does not have a uniform standard, there are 4 spaces, some 8 spaces);
- Control the length of each line of code with a maximum of 80 characters;
- When the code breaks, the following rules apply:
- Break a break after a comma;
- Disconnect before the operator;
- Use a higher level of abstraction to break a break;
- When the line is broken, the logical processing level of each row is at the same level.
- If the above principle causes the code to be confusing, a new line will start directly from the 8th space at the beginning of the previous line;
- two major categories of annotations, implementation comments (/*...*/and//) and Documentation Comments (/**...*/); four small types of annotations, block annotations (for file descriptions, methods, data structures, and algorithms), Single line comment, line end comment, and end line comment;
- Annotations are used to describe abstractly the code outline and information that cannot be obtained directly from the code (such as business logic). Comments should be used to give overviews of code and provide addtional information that's not readily available in the C Ode itself.
- Comments should contain only information that helps to read and understand the program. Comments should contain only information that's relevant to reading and understanding the program.
- Comments should be specific to the implementation of the Code, and will not be obsolete because of code optimization. Avoid comments that is likely to get out of date as the code evlves.
- Adding comments is not a good way to improve the readability of your code, first of all to make the logic of the code itself clearer. If you feel compelled to add a comment, consider rewriting the code to make it clearer.
- A row of a variable or method declaration, variable type and variable name interval between a space;
- Place the statement as far ahead as possible;
- initialization of variable declarations;
- Class and interface declarations are subject to the following rules:
- There is no space between the opening parenthesis of the method name and the parameter name;
- Method declaration The opening curly brace is the same line as the method name;
- Method declaration ends with a single row of closing curly braces;
- A blank line between the method declarations;
- One sentence at a line;
- Use "{...}" Constrained function blocks, especially if-else
- The return statement avoids the use of the method unless the returned result of the method is very clear;
- if-else Logical block notation:
-
if (condition) { statements; } if (condition) { statements; } else { statements; } if (condition) { statements; } else if (condition) { statements; } else if (condition) { statements; } |
- for logical block notation:
-
for ( initialization ;  condition ;  update statements } | /tr>
- while logical block notation:
-
while ( condition " Span style= "font-size:9pt") { < Span style= "font-size:9pt" > statements ; } | /tr>
- Do-while Logical block notation:
-
Do { statements; } while (condition); |
- Switch Logical block notation:
-
switch (condition) { Case ABC: statements; / * Falls through * /Case DEF: statements; Break ;Case XYZ: statements; Break ;Default: statements; Break ;} |
- try-catch Logical block notation:
-
try { statements ; } catch ( Exceptionclass e) { statements ; } | /tr>
- Use of Blank lines:
- Use two empty lines where: different source code blocks; Different classes and interfaces are defined
- Where a blank line is used: A different method, a variable declaration and use in the same method, a block or a single line of comments before the start of a comment, and a different logical block of the same method
- Use of spaces:
- Between the keywords and the parentheses;
- After the comma in the argument list;
- Except for "." All two-dollar operators except;
- for (EXPR1; expr2; expr3);
- After forcing type conversion
- naming rules:
-
- interface Name: class name;
- method name: verb or verb group First letter lowercase;
- variable: single word, all lowercase, multiple word combinations, The first letter is lowercase; the temporary variable generally uses a single letter;
- constant: Use underscores to connect multiple words ; all uppercase letters
- Other:
- Use fixme and xxx;
- Use parentheses to separate simple logical operations, such as "(x >= 0)? ×:-X "and" if ((a = = b) && (c = = d)) "
- Avoid combining assignments to make logic clear and simple! such as "a = B = C" and "if (c + + = d++) {...}"
- Use constants or variables instead of using numbers directly;
Java Coding Specification