Java Coding Convention-asp.net关注
In the guideline sections the terms must, should and can have special meaning. A must requirement must be followed, a should is a strong recommendation, and a can is a general guideline.
Names representing packages should be in all lower case.
java.util
Names representing types must be nouns and written in mixed case starting with upper case.
ArrayList
Variable names must be in mixed case starting with lower case.
list, myList
Names representing constants (final variables) must be all uppercase using underscore to separate words.
COLOR_RED, COLOR_BLUE
Associated constants (final variables) should be prefixed by a common type name.
Names representing methods must be verbs and written in mixed case starting with lower case.
equals(), toString()
Abbreviations and acronyms should not be uppercase when used as name.
UserDao // NOT: UserDAO
getUserId() // NOT: getUserID()
userId // NOT: userID
Variables with a large scope should have long names, variables with a small scope can have short names.
The name of the object is implicit, and should be avoided in a method name.
line.getLength(); // NOT: line.getLineLength();
is prefix should be used for boolean variables and methods.
isFound, isExpired
Negated boolean variable names must be avoided.
boolean isFound; // NOT: isNotFound
boolean isExpired; // NOT: isNotExpired
Setter methods for boolean variables must have set prefix as in:
void setFound(boolean isFound);
There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes:
boolean hasLicense();
boolean canEvaluate();
Plural form should be used on names representing a collection of objects.
List<User> users;
User[] users;
Iterator variables should be called i, j, k etc.
for (Iterator i = users.iterator(); i.hasNext();) {
...
}
for (int i = 0; i < users.length; i++) {
...
}
Exception classes should be suffixed with Exception.
AccessException, BackendException
Singleton classes should return their sole instance through method getInstance.
The opening bracket should be put at the end of the same line of the statement.
if (isFound) {
doSomething();
}
Should use spaces instead of Tab.
File content must be kept within 80 columns.
The incompleteness of split lines must be made obvious.
In general:
* Break after a comma.
* Break after an operator.
* Align the new line with the beginning of the expression on the previous line.
Imported classes should always be listed explicitly.
import java.util.List; // NOT: import java.util.*;
Variables should be kept alive for as short a time as possible.
Loop variables should be initialized immediately before the loop.
isDone = false; // NOT: bool isDone = false;
while (!isDone) { // :
: // while (!isDone) {
} // :
// }
Always use brackets for if statement.
if (isFound) { // NOT: if (isFound) doSomething();
doSomething();
}
Floating point constants should always be written with decimal point and at least one decimal.
double total = 0.0; // NOT: double total = 0;
Operators should be surrounded by a space character.
a = (b + c) * d;
Java reserved words should be followed by a white space.
while (true)
Commas should be followed by a white space.
doSomething(a, b, c, d);
Comment identifier should be followed by a white space.
// This is a comment NOT: //This is a comment
Names representing packages should be in all lower case.
java.util
Names representing types must be nouns and written in mixed case starting with upper case.
ArrayList
Variable names must be in mixed case starting with lower case.
list, myList
Names representing constants (final variables) must be all uppercase using underscore to separate words.
COLOR_RED, COLOR_BLUE
Associated constants (final variables) should be prefixed by a common type name.
Names representing methods must be verbs and written in mixed case starting with lower case.
equals(), toString()
Abbreviations and acronyms should not be uppercase when used as name.
UserDao // NOT: UserDAO
getUserId() // NOT: getUserID()
userId // NOT: userID
Variables with a large scope should have long names, variables with a small scope can have short names.
The name of the object is implicit, and should be avoided in a method name.
line.getLength(); // NOT: line.getLineLength();
is prefix should be used for boolean variables and methods.
isFound, isExpired
Negated boolean variable names must be avoided.
boolean isFound; // NOT: isNotFound
boolean isExpired; // NOT: isNotExpired
Setter methods for boolean variables must have set prefix as in:
void setFound(boolean isFound);
There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes:
boolean hasLicense();
boolean canEvaluate();
Plural form should be used on names representing a collection of objects.
List<User> users;
User[] users;
Iterator variables should be called i, j, k etc.
for (Iterator i = users.iterator(); i.hasNext();) {
...
}
for (int i = 0; i < users.length; i++) {
...
}
Exception classes should be suffixed with Exception.
AccessException, BackendException
Singleton classes should return their sole instance through method getInstance.
The opening bracket should be put at the end of the same line of the statement.
if (isFound) {
doSomething();
}
Should use spaces instead of Tab.
File content must be kept within 80 columns.
The incompleteness of split lines must be made obvious.
In general:
* Break after a comma.
* Break after an operator.
* Align the new line with the beginning of the expression on the previous line.
Imported classes should always be listed explicitly.
import java.util.List; // NOT: import java.util.*;
Variables should be kept alive for as short a time as possible.
Loop variables should be initialized immediately before the loop.
isDone = false; // NOT: bool isDone = false;
while (!isDone) { // :
: // while (!isDone) {
} // :
// }
Always use brackets for if statement.
if (isFound) { // NOT: if (isFound) doSomething();
doSomething();
}
Floating point constants should always be written with decimal point and at least one decimal.
double total = 0.0; // NOT: double total = 0;
Operators should be surrounded by a space character.
a = (b + c) * d;
Java reserved words should be followed by a white space.
while (true)
Commas should be followed by a white space.
doSomething(a, b, c, d);
Comment identifier should be followed by a white space.
// This is a comment NOT: //This is a comment