Java编码规范(部分)

来源:互联网 发布:链接买卖源码 编辑:程序博客网 时间:2024/05/21 07:39
Java编码规范
 
Do it right the first time.
第一次就应当做好。
 
Break up long lines.
拆分长行。
 
Do not use "hard" tabs.
不要使用tab键。
 
Join the vowel generation.
不要省略元音字母。
 
Capitalize only the first letter in acronyms.
只对简称的第一个字母大写。
 
Capitalize the first letter of each word that appears in a class or interface name.
类和接口名称只对每个单词的第一个字母大写。
 
Use nouns when naming classes.
使用名词来命名类。
 
Use nouns or adjectives when naming interfaces.
使用名词或形容词来命名接口。
 
Use lowercase for the first word and capitalize only the first letter of each subsequent word that appears in a method name. 方法名称中第一个单词小写,后续的每一个单词仅第一个字母大写。
 
Use verbs when naming methods.
使用动词来命名方法。
 
Use lowercase for the first word and capitalize only the first letter of each subsequent word that appears in a variable name.
变量名称中第一个单词小写,后续的每一个单词仅第一个字母大写。
 
Use nouns to name fields.
使用名词来命名成员变量。
 
Establish and use a set of standard names for trivial "throwaway" variables.
对于通常的临时变量,建立并使用一套标准名称。
 
Qualify field variables with "this" to distinguish them from local variables.
使用“this”限定成员变量以同局部变量区分。
 
 When a constructor or "set" method assigns a parameter to a field, give that parameter the same name as the field.当构造函数或者“set”方法向成员分配参数时,参数的命名应和成员相同。
Use uppercase letters for each word and separate each pair of words with an underscore when naming constants.当命名常量时,每个单词均大写,单词之间以下划线区分。
 
Add internal comments only if they will aid others in understanding your code.
只有当内部注释有助于其他人理解你的代码时才添加。
 
Avoid the use of end-line comments.
避免使用行尾注释。
 
 Explain local variable declarations with an end-line comment.
使用行尾注释解释局部变量声明。
 
Consider declaring classes representing fundamental data types as final.
考虑将表示基本数据的类声明为final。
 
Define small classed and small methods.
定义更小的类和方法。
 
Make all fields private.
使所有成员变量私有。
 
Wrap general-purpose classes that operate on java.lang.Object to provide static type checking.
对操作对象为java.lang.Object的通用类进行包装,以提供静态类型检查。
 
Encapsulate enumerations as classes.
以类的形式封装枚举型。
 
Replace repeated nontrivial expressions with equivalent methods.
将重复出现的、有点复杂的表达式抽取为方法。
Always code a break statement in the last case of a switch statement.
在switch语句最后一个case中总是键入break语句。
Use equals(), not ==, to test for equality of objects.
使用equals()而不是==来测试对象的相等性。
Do not call nonfinal methods from within a constructor.
不要在构造函数中调用非final方法。
Do not silently absorb a run-time or error exception.
不要略去运行时或错误异常。
Use a finally block to release resources.
使用finally块以释放资源。
Use threads only where appropriate.
仅在适当时使用线程。
 
Use lazy initialization.
使用延迟初始化。
 
Avoid creating unnecessary objects.
避免创建不必要的对象。
 
Reinitialize and reuse objects to avoid new object construction.
重复初始化和重复使用对象以避免创建新的对象。
  
Place types that are commonly used, changed, and released together, or mutually dependent on each other, into the same package.
把经常使用、变化和一起发布或者彼此互相依赖的类放置在相同的包中。

 

 

 
原创粉丝点击