Genaral Principles 一般約定
1.Adhere to the style
of the original.
保持最初的樣式。
2.Adhere to the Principle of Least
Astonishment.
遵守最小驚奇原則。
3.Do it right the first time.
第一次就應當做好。
4.Document any deviations.
對於任何背離都應當文檔注釋。
Formatting
Conventions 格式約定
5.Indent nested code.
約束嵌套代碼。
6.Break up long lines.
拆分長行。
7.Include white space.
包含空格。
8.Do not use "hard" tabs.
不要使用tab鍵。
Naming
Conventions 命名規範
9.Use meaningful names.
使用有意義的名稱。
10.Use familiar names.
使用熟悉的名稱。
11.Question excessively
long names.
不要使用過長的名稱。
12.Join the vowel generation.
加入母音字母。
13.Capitalize only the first letter in acronyms.
只對簡稱的第一個字母大寫。
14.Do not use names that differ only in case.
不要使用只依賴於大小寫來區分的名稱。
Package Names 包命名
15.Use
the reversed, lowercase form of your organization's Internet domain
name as the root qualifier for your package names.
採用你組織的Internet網域名稱
的反轉、小寫形式作為包名稱的根限定詞。
16.Use a single, lowercase word as the root
name of each package.
使用一個唯一的小寫單詞作為每個包的根名稱。
17.Use the
same name for a new version of a package, but only if that new version
is still binary compatible with the previous versin, otherwise, use a
new name.
只有當包的新版本仍然與舊版本相容時,對於包的新版本使用相同的名稱,否則使用新名稱。
Type Names 類型命名
18.Capitalize the first letter of
each word that appears in a class or interface name.
對於類和介面名稱只對每個單詞
的第一個字母大寫。
Class Names 類命名
19.Use
nouns when naming classes.
使用名詞來命名類。
20.Pluralize the
names of classes that group related attributes, static services, or
constants.
對於成組的相關屬性、靜態服務和常量,類名稱採用複數形式。
Interface
Names 介面命名
21.Use nouns or adjectives when naming
interfaces.
使用名詞或形容詞來命名介面。
Method Names 方法命名
22.Use
lowercase for the first word and capitalize only the first letter of
each subsequent word that appears in a method name.]
方法名稱中第一個單詞小寫,後
續的每一個單詞僅第一個字母大寫。
23.Use verbs when naming methods.
使用動詞來命名
方法。
24.Follow the JavaBeans conventions for naming property
accessor methods.
命名屬性存取方法遵守JavaBeans約定。
Variable
Names 變數命名
25.Use lowercase for the first word and
capitalize only the first letter of each subsequent word that appears in
a variable name.
變數名稱中第一個單詞小寫,後續的每一個單詞僅第一個字母大寫。
26.Use
nouns to name fields.
使用名詞來命名成員變數。
27.Pluralize the names
of collection reference.
對於集合引用的名稱採用複數形式。
28.Establish
and use a set of standard names for trivial "throwaway" variables.
對
於通常的臨時變數,建立並使用一套標準名稱。
Field Names 成員命名
29.Qualify field variables with "this" to distinguish them from local
variables.
使用“this”限定成員變數以同局部變數區分。
Parameter
Names 參數命名
30.When a constructor or "set" method
assigns a parameter to a field, give that parameter the same name as the
field.
當建構函式或者“set”方法向成員分配參數時,參數的命名應和成員相同。
Constant
Names 常量命名
31.Use uppercase letters for each word and
separate each pair of words with an underscore when naming constants.
當
命名常量時,每個單詞均大寫,單詞之間以底線區分。
Documentation Conventions 文檔約定
32.Write documentation for those who must use your code and those must
maintain it.
為那些使用你代碼和維護它的人寫文檔。
33.Keep comments and code
in sync.
保持注釋與代碼同步。
34.Use the active voice and omit
needless words.
使用積極的語氣,忽略無用的單詞。
Comment Types 注釋類型
35.Use
documentation comments to describe the programing interface.
使用文檔注
釋來描述程式介面。
36.Use standard comments to hide code without
removing it.
使用標準注釋來隱藏代碼而不是刪除。
37.Use one-line comments to
explain implementation details.
使用單行注釋來解釋實現細節。
Documentation
Comments 文檔注釋
38.Describe the programing interface
before you write the code.
在寫代碼之前描述程式介面。
39.Document
public, protected, package, and private members.
對public、protected、
package和private成員進行文檔注釋。
40.Provide a summary description and
overview for each package.
對每一個包提供概述。
41.Provide a
summary description and overview for each application or group of
packages.
對每一個應用或者包的組提供概述。
Comment Style 注釋樣式
42.Use a single consistent format and organization for all
documentation comments.
對於所有的文檔注釋堅持使用一種格式。
43.Wrap
keywords, identifiers, and constants with <code>...</code>
tags.
使用<code>...</code>標記封裝關鍵字、識別符和常量。
44.Wrap code with <pre>...</pre> tags.
使用<pre>...</pre>標記封裝代碼。
45.Consider marking the
first occurrence of an identifier with a {@link} tag.
考慮在標識符第一次出現時使用{@link}標記。
46.Establish and use a fixed ordering
for Javadoc tags.
對於Javadoc標記應該建立並使用一種固定的順序。
47.Write
in the third-person narrative form.
以第三人稱的敘述形式寫。
48.Write summary descriptions that stand alone.
寫概述應該獨立。
49.Omit the subject in summary descriptions of actions or services.
在概述中忽略目標。
50.Omit the subject and the verb in summary
descriptions of things.
在事物的概述中忽略目標和動詞。
51.Use "this"
rather than "the" when referring to instance of current class.
當指代當前類的執行個體時,用“this”而不是“the”。
52.Do not add parentheses to a
method or constructor name unless you want to specify a particular
signature.
不要對方法或建構函式名增加括弧,除非你想表示特定的簽名。
Comment
Content 注釋內容
53.Provide a summary description for
each method.
給每一個方法提供概述。
54.Fully describe the signature
of each method.
完整描述每一方法簽名。
55.Include examples.
包含樣本。
56.Document preconditions, postconditions, and invariant
conditions.
對前置、後置、不變條件進行文檔注釋。
57.Document known
defects and deficiencies.
對已知的缺陷和不足進行文檔注釋。
58.Document synchronization semantics.
對同步文法進行文檔注釋。
Internal Comments 內部注釋
59.Add internal comments only
if they will aid others in understanding your code.
只有當內部注釋有助於其他人理解
你的代碼時才添加。
60.Describe why the code is doing what it does, not
what the code is doing.
描述代碼為什麼那樣做,而不是做了什麼。
61.Avoid the
use of end-line comments.
避免使用行章節附註釋。
62.Explain local
variable declarations with an end-line comment.
使用行章節附註釋解釋局部變數聲明。
63.Establish
and use a set of keywords to flag unresolved issues.
建立並使用一套關鍵字來標記
未確定的情況。
64.Label closing braces in highly nested control
structures.
在嵌套程度高的控制結構中標記結束。
65.Add a "fall-through"
comment between two case labels, if no break statement separates those
labels.
在兩個case標籤中,如果沒有break語句分隔這些標籤,加入“fall-through”注釋。
66.Label
empty statements.
標記空語句。
Programming Convertions
編碼約定
67.Consider declaring classes representing
fundamental data types as final.
考慮將表示基本資料的類聲明為final。
68.Build concrete types from native types and other concrete types.
從本地類型和其它具體類型構建具體類型。
69.Define small classed and small methods.
定義更小的類和方法。
70.Define subclasses so they may be used anywhere
their superclasses may be used.
定義子類使之可以在父類使用的任何地方使用。
71.Make all fields private.
使所有成員變數私人。
72.Use polymorphism
instead of instanceof.
使用多態來替代instanceof。
Type
Safety型別安全
73.Wrap general-purpose classes that
operate on java
.lang.Object
to provide static type checking.
以操作java
.lang.Object
來封裝通用類,提供靜態類型檢查。
74.Encapsulate enumerations as classes.
以
類的形式封裝枚舉型。
Statements and Expressions 運算式
75.Replace
repeated nontrivial expressions with equivalent methods.
使用相當的方法來
替換重複出現的、有點複雜的運算式。
76.Use block statements instead of
expression statements in control flow constructs.
使用塊狀語句來替代控制流程結構的表
達式。
77.Clarify the order of operations with parentheses.
使
用括弧來明確運算順序。
78.Always code a break statement in the last case
of a switch statement.
在switch語句最後一個case中總是鍵入break語句。
79.Use
equals(), not ==, to test for equality of objects.
使用equals()而不是==
來測試對象的相等性。
80.Always construct objects in a valid state.
總
是構建狀態有效對象。
Construction 建構函式
81.Do
not call nonfinal methods from within a constructor.
不要在建構函式中調用非
final方法。
82.Use nested constructors to eliminate redundant
code.
使用嵌套建構函式來消除冗餘代碼。
Exception Handling異常處理
83.Use
unchecked, run-time exceptions to report serious unexpected errors that
may indicate an error in the program's logic.
使用不需檢查運行時異常來報告嚴重的、無法
預期的錯誤,這可能指出程式邏輯中的錯誤。
84.Use checked exceptions to report errors
that may occur, however rarely, under normal program operation.
使用
需檢查異常來報告可能發生但是在正常程式操作中很難發生的錯誤。
85.Use return codes to report
expected state change.
使用return代碼來報告期望的狀態改變。
86.Only
convert exceptions to add information.
僅以增加資訊形式來改變異常。
87.Do
not silently absorb a run-time or error exception.
不要略去運行時或錯誤異常。
88.Use
a finally block to release resources.
使用finally塊以釋放資源。
Assertions 斷言
89.Program by contract.
編程遵守約定。
90.Use
dead code elimination to implement assertions.
使用無效代碼清除策略實現斷言。
91.Use
assertions to catch logic errors in your code.
使用斷言捕獲代碼中的邏輯錯誤。
92.Use
assertions to test pre- and postconditions of a method.
使用斷言測試方法的前
置和後置條件。
Concurrency 並發
93.Use threads
only where appropriate.
僅在適當時使用線程。
Synchronization
同步
94.Avoid synchronization.
避免同步。
95.Use
synchronized wrappers to provide synchronized interfaces.
使用同步封裝來提
供同步介面。
96.Do not synchronize an entire method if the method
contains significant operations that do not need synchronization.
如
果方法包含幾個不需要同步的重要步驟,那麼不要同步整個方法。
97.Avoid unnecessary
synchronization when reading or writing instance variables.
當讀寫執行個體變數
時避免不必要的同步。
98.Consider using notify() instead of notifyAll().
考
慮用notify()替代notifyAll()。
99.Use the double-check pattern for
synchronized initialization.
對於同步的初始化應使用雙重檢查模式。
Efficiency 效率
100.Use lazy initialization.
使用延遲初始化。
101.Avoid creating unnecessary objects.
避免建立不必要的對
象。
102.Reintialize and reuse objects to avoid new object
construction.
重複初始化和重複使用對象以避免建立新的對象。
103.Leave
optimization for last.
留待以後最佳化。
Packaging Conventions
包約定
104.Place types that are commonly used, changed,
and released together, or mutually dependent on each other, into the
same package.
把經常使用、變化和發布或者彼此互相依賴的的類放置在相同的包中。
105.Isolate
volatile classes and interfaces in separate packages.
將volatile類和介面隔離在單獨的包中。
106.Avoid making packages that are
difficult to change dependent on packages that are easy to change.
避免建立的包,依賴於經常變化的包,使其很難變化。
107.Maximize abstraction to maximize
stability.
最大的抽象化以達到最大的穩定性。
108.Capture high-level design
and architecture as stable abstractions organized into stable package.
採用高水平的設計和架構使包保持穩定。