If you need to copy, spread, please attach this statement, thank you. Original source: http://java.sun.com/docs/codeconv/html/codeconvtoc.doc.html, the translation source: http://morningspace.51.net/ ,moyingzz@etang.com
(Continued) 8 blank (White Space)
8.1 Blank Lines
The rule is separated from the logical code segment to improve readability. The following conditions should always use two space lines: - a source file between two sections - class declarations and interface declarations should always use an empty line: - Between two methods - method Between the first statements of local variables and methods - block comments (see "5.1.1") or single-line comments (see "5.1.2") - between two logic segments within a method, Improve readability
8.2 spaces (Blank Spaces)
The following conditions should be used in space: - a keyword that keeps with parentheses should be separated by spaces, for example: while {
...
}
Note: The space should not be placed between the method name and the left brackets. This will help distinguish between keywords and method calls. - Blank should be in the back of the comma in the parameter list - all binary operators, except ".", You should use spaces to separate from the operand. The one-dollar operator and the operand is not due to the splitter, such as: negative (" ") and self-reduction ("-"). For example: A = C D;
A = (a b) / (c * d);
While (D = s ) {
N ;
}
PRINTSIZE ("Size IS" FOO "/ N");
- The expression in the for statement should be separated by spaces, for example: for (expr1; expr2; expr3)
- It should be followed by a space after compulsory transformation, for example: MyMethod ((Byte) Anum, (Object) x);
MyMethod ((int) (CP 5) ((int) (I 3)) 1);
9 Name Specification (Naming Conventions)
Naming specification makes the program more readily, making it easier to understand. They can also provide information about identifier functions to help understand code, for example, whether it is a constant, package, or class.
Identifier Type Name Rules Example Pack (Packages) A unique package prefix is always lower-write ASCII letters and is a top domain name, usually COM, EDU, GOV, MIL, NET, ORG, or 1981 ISO 3166 Standard Specified English Double Character Code in the country. The subsequent part of the package is not the same as the naming specification within the respective mechanisms. Such naming specifications may distinguish between departments, projects, or registration names in a specific directory name. com.sun.engcom.apple.quicktime.v2edu.cmu.cs.bovik.chese class (Classes) Names Rules: Classified is a word, using case-by-case mixing, the first letter of each word. Try to make your class name simple and description. Use intact words to avoid abbreviations (unless the abbreviation words are more widely used, like url, html) class rasser; Class ImageSprite; interface (interfaces) Naming rules: case-sensitive rules are similar to Interface RasterDelegate; interface storing; method Methods) method name is a verb, which uses cases of cases mixed, and the first letter of the first word is lowercase, the first letter of the words. Run (); Runfast (); getBackground (); variable (variables) In addition to the variable name, all instances, including classes, types, usual, using case-write mix, the first letter of the first letter, after the word The initial letters. The variable name should not begin with the scribe or dollar symbol, although this is allowed in grammar. The variable name should be short and described. The selection of the variable name should be easy to remember, that is, it can be pointed out. Try to avoid a variable name of a single character unless it is a disposable temporary variable. Temporary variables are usually named I, J, K, M and N, which are generally used for integer; C, D, E, which are generally used for characters. CHAR C; INT i; FLOAT MYWIDTH; instance variable (instance variables) case, in addition to the number of variable names, in addition to a downline int _employeid; string _name; Customer _CUSTOMER; constant (constants), a statement, and ANSI constants, should All uppercase, interlaced interlaced in words. (Try to avoid ANSI constants, easy to cause errors) Static final int min_width = 4; static final int max_width = 999; static final int GET_THE_CPU = 1; 10 Programming Formula (Programming Practices)
10.1 Provides access control for examples and class variables (Providing Access To Instance and Class Variables)
If there is not enough reason, don't declare an instance or class variable as public. Typically, instance variables do not require explicit settings (SET) and gotten, which is usually produced as the edge effect of method calls (Side Effect). A proper example of a public instance variable is that the class is only the data structure, no behavior. That is, if you want to use a structure rather than a class (if the Java supports the structure), then the instance variable declaration of the class is suitable.
10.2 Reference Class Variables and Class Methods (Referring to Class Variables and Methods)
Avoid access to a class of static variables and methods with an object. You should be replaced by a class name. For example: classMethod (); //okaclass.classmethod (); // ok
Anobject.classMethod (); // avoid!
10.3 Constants
In addition to the digital constant of the counter value in the FOR cycle, in addition to -1, 0 and 1, it should not be written directly to code.
10.4 variable assignment (variable assignments)
Avoid value to multiple variables in one statement. It is hard to read. For example: foobar.fchar = barfoo.lchar = 'c'; // Avoid!
Do not use the assignment operator to be confused with the equivalent relationship operator. For example: IF (C = D ) {// Avoid! (Java Disallows)
...
}
It should be written into IF ((C = D )! = 0) {
...
}
Do not use embedded assignment operators to try to improve the efficiency of runtime, which is the work of the compiler. For example: d = (a = b c) r; // avoid!
It should be written as a = B C;
D = a r;
10.5 Other Practices (Miscellaneous Practices)
10.5.1 Parentheses (PARENTHESES)
In general, the use of parentheses in an expression containing a variety of operators to avoid operator priority issues, it is a good way. Even if the priority of the operator may be clear, it is not necessary for others. You can't assume that other programmers are as clear as you know the priority of the operator. IF (a == b && c == d) // Avoid!
IF ((a == b) && (c == d)) // Right
10.5.2 Return Values
Try to make your program structure in accordance with the purpose. For example: if (booleaneXpression) {
Return True;
} else {
Return False;
}
It should be in this way as follows: Return BooleaneXpression;
Similarly: IF (condition) {
Return X;
}
Return Y;
Should be written: return (condition? X: y);
10.5.3 Conditional Operators "?" Expressions Before '?' In The Conditional Operator
If an expression containing a binary operator appears in the ternary operator "?:", Then a pair of parentheses should be added to the expression. For example: (x> = 0)? X: -x;
10.5.4 Special Comments
Using XXX in the comment to identify some of the content that does not implement (BOGUS) but works. Use fixme to identify some fake and error content.
11 Code Examples (CODE EXMPLES)
11.1 Java Source File Example (Java Source File Example)
The following example shows how to reasonably layout a Java source program containing a single public class. The layout of the interface is similar. See "Class and Interface Declaration" and "Text Refidence". / *
* @ (#) Blah.java 1.82 99/03/18 *
* CopyRight (C) 1994-1999 Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All Rights Reserved.
*
* This Software Is The Confidential and ProPrietary Information of Sun
* Microsystems, Inc. ("Confidential Information"). You Shall NOT
* Disclose Such Confidential Information and Shall Use it Only inLy Information and Shall USE INLY IN
* Accordance with the Terms of the license agreement you entered Into
* with sun.
* /
Package java.blah;
Import java.blah.blahdy.blahblah;
/ **
* Class Description Goes Here.
*
* @version 1.82 18 Mar 1999
* @Author firstname lastname
* /
Public class blah extends someclass {
/ * A class implementation common can go here. * /
/ ** Classvar1 Documentation Comment * /
Public static int classvar1;
/ **
* Classvar2 Documentation Comment That Happens To BE
* More Than One Line Long
* /
Private static object classvar2;
/ ** Instancevar1 Documentation Comment * /
Public Object Instancevar1;
/ ** Instancevar2 Documentation Comment * /
Protected int instancevar2;
/ ** Instancevar3 Documentation Comment * /
Private object [] instancevar3;
/ **
* ... Constructor Blah Documentation Comment ...
* /
Public blah () {
// ... Implementation Goes Here ...
}
/ **
* ... Method Dosomething Documentation Comment ...
* /
Public void dosomething () {
// ... Implementation Goes Here ...
}
/ **
* ... Method Dosomethingelse Documentation Comment ...
* @Param SomeParam Description
* /
Public void Dosomethingelse (Object SomeParam) {
// ... Implementation Goes Here ...
}
}
(Finish)