對于剛?cè)胄谢蛘呷胄袔啄甑某绦騿T,或多或少,都有一點(diǎn)代碼規(guī)范的問題,有些是沒法子,各種需求變更,或入手各種別人寫了一半的代碼等等,因?yàn)闀r間問題,有時候你是心有余而力不足啊。雖然如此,我們還是得把代碼規(guī)范養(yǎng)成一種習(xí)慣,這不僅僅是寫出優(yōu)美的代碼,好的代碼還能提高效率和提升性能的
一、包
包名全部采用小寫,不用下劃線區(qū)分單詞
主包名采用[公司性質(zhì)].[公司名稱].[項(xiàng)目名稱]的命名方式
例如:翡翠教育做的手機(jī)助手包名edu.feicui.assistant-
通用功能子包名采用[主包名].[通用名稱]的命名方式
常見通用功能如下表:
一般功能子包名采用[主包名].[模塊名稱].[子模塊名稱]的命名方式,例如:包.png
手機(jī)助手的鬧鐘模塊包名 edu.feicui.assistant.alarm 只需導(dǎo)入用到的類,不得用*導(dǎo)入包下所有類
導(dǎo)入類時,系統(tǒng)類在上方,自定義類在下方
二、代碼
代碼主要采用大/小駝峰命名法,即除首字母外,每個單詞首字母大寫,整體首字母大小根據(jù)其它規(guī)范決定
類名、接口名、枚舉名等首字母大寫,若由多個單詞組成,則其后每個單詞首字母大寫,例如:
class ConfigManager{}類名、接口名、枚舉名等首字母大寫,若由多個單詞組成,則其后每個單詞首字母大寫,例如:
class ConfigManager{}繼承自安卓組件的類,采用父類名作為后綴,例如:
class LoginActivity extends Activity{}自定義異常必須以Exception結(jié)尾
除for循環(huán)變量外,一律不得使用i、j、k等單字符作為變量名
定義數(shù)組時方括號緊隨在原始類型之后,數(shù)組名稱一般使用復(fù)數(shù)形式,例如:
int[] arrays;常量、枚舉等均采用大寫形式,用下劃線區(qū)分各單詞,例如:
final static int DIALOG_ID_ALARM = 1;
enum Season{SPRING, SUMMER, AUTUMN, WINTER};全局變量添加所有者前綴:實(shí)例成員變量前綴m(表示member),類靜態(tài)變量前綴s(表示static),例如:
實(shí)例變量mRun
類靜態(tài)變量sInstance-
控件變量添加組件前綴,順序在所有者前綴之后,例如:
全局名稱mBtnNext
局部名稱btnNext
常見控件前綴如下表:
空間前綴.jpg 除單例模式外一般不得使用靜態(tài)變量
常量一般使用final static修飾,根據(jù)需要使用可見性修飾符,例如:
public static final int VISIBLE = 0x00000000;一般方法名首字母小寫,若由多個單詞組成,則其后每個單詞首字母大寫
構(gòu)造方法采用遞增方式(參數(shù)多的寫在后面)
例如:
public GameView(Context context) {
this(context, null);
}
public GameView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GameView(Context context, AttributeSet attrs, int defStyle) {}僅在項(xiàng)目內(nèi)使用的實(shí)體類不使用JavaBean進(jìn)行封裝,直接將成員變量訪問修飾符修改為非private,例如:
class User{public String name,pwd;}實(shí)體類中固定值的成員變量可設(shè)置成final,并通過構(gòu)造函數(shù)初始化
實(shí)體類中不得隨意修改的成員變量可添加下劃線前綴以作區(qū)別,例如:
class User{public int _id;}一般不使用System.out輸出,而是使用Log中的方法
使用BuildConfig.DEBUG標(biāo)記對Log進(jìn)行封裝,只在調(diào)試時輸出重要信息,正式版不輸出
一般try……catch只捕獲需要的異常
catch塊不得為空,至少應(yīng)當(dāng)將異常信息輸出
三、資源
資源命名全部采用小寫,各單詞間以下劃線區(qū)分
-
布局文件采用[前綴]_[功能模塊].xml的命名方式,例如:
MainActivity的布局activity_main.xml
常見前綴如下表:
常見前綴.png -
圖片采用[性質(zhì)前綴][功能模塊] [屬性后綴].[擴(kuò)展名]的方式,例如:
主背景圖片bg_main.png
確定按鈕按下btn_ok_presssed.png
常見屬性后綴如下表:
常見屬性后綴.png -
values目錄下文件名稱較固定,不得隨意更改,常見名稱如下:
常見名稱.png
四、注釋
開源項(xiàng)目必須添加文件注釋,非開源項(xiàng)目建議添加,例如:
/*
* @(#): Document.java
* @project:IndentObjectNatation
* @version: v1.1
* @copyright: Copyright (C) 2013-2014 The Emerald Education
* @description:
* This file is a part of Indent Object Notation project.
*
* @modify:
* ---- No.1 Modified By Mr. Tang At 2014-05-06 11:32 Based On 1.0 ----
* Create this file.
* ---- No.2 Modified By Mr. Zhang At 2014-05-06 11:32 Based On 1.0 ----
* Make the class Document extend from the class Node.
*/類定義一般需要寫類注釋,接口一般需要寫接口注釋,如果沒有文件注釋,則需要在類注釋和接口注釋中標(biāo)出作者,例如:
/**
* Root of the ION tree, provides the access to the document's data.
* <p>
* Subclass of {@link Node}, Since elements, comments, etc. cannot exist
* outside the context of a Document, the Document also contains the
* factory methods needed to create these objects.
* </p>
*
* @author Mr. Zhang
*/
class Document {}成員變量、靜態(tài)變量、常量等添加屬性注釋,例如:
/** This view is invisible. */
public static final int INVISIBLE = 0x00000004;關(guān)聯(lián)性較大的多個成員變量等可以共用同一條注釋,例如:
/** The width and height of View. */
private int mWidth, mHeight;public和protected方法必須添加方法注釋,default和private方法建議添加方法注釋,例如:
/**
* Writes {@code count} characters starting at {@code offset} in {@code buf}
* to the target.
*
* @param buf
* the non-null character array to write.
* @param offset
* the index of the first character in {@code buf} to write.
* @param count
* the maximum number of characters to write.
* @return {@code true} if success, otherwise {@code false}
* @throws IndexOutOfBoundsException
* if {@code offset < 0} or {@code count < 0}, or if {@code
* offset + count} is greater than the size of {@code buf}.
* @throws IOException
* if this writer is closed or another I/O error occurs.
*/
public abstract boolean write(char[] buf, int offset, int count) throws IndexOutOfBoundsException, IOException;若覆蓋基類的方法,則可以不寫方法注釋,但必須用@Override標(biāo)出,例如:
@Override
protected void onCreate(Bundle savedInstanceState) {}不建議繼續(xù)使用的方法用@Deprecated標(biāo)出
switch……case的每個條件一般添加簡短說明,例如:
switch (type) {
case 1:// Android apps
break;
case 2:// Android games
break;
case 3:// iOS apps
break;
default:// Not a valid package
break;
}如果if的條件大于2個,則必須寫注釋,例如:
if (isBluetooth // If Bluetooth network is on
|| isWifi // If WLAN network is on
|| is3g // if 3g network is on
) {}對于未完成的方法,使用TODO加以標(biāo)記,例如:
void write(byte[] buf, File file) {
// TODO: Write buf to file
}若功能已完成,但存在效率等潛在問題時,使用XXX加以標(biāo)記,例如:
void parseXML(File file) {
// XXX: Maybe SAX is better
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(file);
}若代碼存在嚴(yán)重問題或僅用于調(diào)試,使用FIXME加以標(biāo)記(注:存在FIXME標(biāo)記的代碼不能作為正式版發(fā)布)
boolean login(String name, String pwd) {
//FIXME: Remove this line before publishing
System.out.println("name=" + name + ", password=" + pwd);
if (users.containsKey(name) && users.get(name).equals(pwd))
return true;
return false;
}如果for、while等代碼塊過長,可以在結(jié)尾處標(biāo)記循環(huán)變量,例如:
for (int position = 0; position < 10; position++) {
……
}// end for: position
while(mRun){
……
}// end while: mRun
leo差不多就介紹這些了,歡迎補(bǔ)充!




