Java Coding Convention-asp.net关注
2011-01-30 14:28
302 查看
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
相关文章推荐
- JAVA动态绑定的内部实现机制(转)-asp.net关注
- 学习java到底好不好?-asp.net关注
- Java 十大人物-asp.net关注
- 一直在关注技术 asp .net delphi java objectc c++ php 以后...
- Java数组与容器类分析资料--数组、List和Set、Map-asp.net关注
- 【引用】Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)-asp.net关注
- java 简单缓存机制-asp.net关注
- java入门2-asp.net关注
- 十大非著名之父:手机,黑莓,iPod,FORTRAN,远程办公,鼠标,垃圾邮件,DSL,Java,WIFI说,我爸是...-asp.net关注
- JAVA IO操作文件(包括word文档的显示)-asp.net关注
- java题集-asp.net关注
- java入门5-asp.net关注
- 使用java程序读取配置文件中的相关属性值-asp.net关注
- java入门4-asp.net关注
- java.sql.SQLException: 关闭的连接 (读取oracle 10g Blob)-asp.net关注
- Java变量影响范围-asp.net关注
- java urlrewrite伪静态-asp.net关注
- Java常用正则表达式-asp.net关注
- Java学习第七课...枚举.自动装箱.静态导入...-asp.net关注
- java的正规表达式的java工具类-asp.net关注