Android 編碼規範 | 代碼風格指南

 一、異常

1.不要忽視異常處理

如果像下面的代碼這樣,對catch後的異常作空處理,就像埋下地雷一樣讓人感覺到毛骨悚然:

錯誤的做法: 

  1. void setServerPort(String value) {   
  2.     try {   
  3.         serverPort = Integer.parseInt(value);   
  4.     } catch (NumberFormatException e) {   
  5.     }   
  6. }   

正確的做法(1):

在方法聲明時拋出異常,由客戶程序員去負責消化這個異常。 

  1. void setServerPort(String value) throws NumberFormatException {   
  2.     serverPort = Integer.parseInt(value);   
  3. }   

正確的做法(2): 

  1. /** Set port. If value is not a valid number, 80 is substituted. */   
  2. void setServerPort(String value) {   
  3.     try {   
  4.         serverPort = Integer.parseInt(value);   
  5.     } catch (NumberFormatException e) {   
  6.         serverPort = 80;  // default port for server   
  7.     }   

正確的做法(3): 

  1. /** Set port. If value is not a valid number, die. */   
  2. void setServerPort(String value) {   
  3.     try {   
  4.         serverPort = Integer.parseInt(value);   
  5.     } catch (NumberFormatException e) {   
  6.         throw new RuntimeException("port " + value " is invalid, ", e);   
  7.     }   

正確的做法(4): 

  1. void setServerPort(String value) throws ConfigurationException {   
  2.     try {   
  3.         serverPort = Integer.parseInt(value);   
  4.     } catch (NumberFormatException e) {   
  5.         throw new ConfigurationException("Port " + value + " is not valid.");   
  6.     }   
  7. }   

2、不要偷懶而捕捉一般異常

下面代碼一概捕捉Exception異常,大小通吃是不對的,這樣會讓你在錯誤出現時難以定位到錯誤原因,一般異常無法用統一方法進行異常處理。

錯誤的做法: 

  1. try {   
  2.     someComplicatedIOFunction();        // may throw IOException   
  3.     someComplicatedParsingFunction();   // may throw ParsingException   
  4.     someComplicatedSecurityFunction();  // may throw SecurityException   
  5.     // phew, made it all the way   
  6. catch (Exception e) {               // I'll just catch all exceptions   
  7.     handleError();                      // with one generic handler!   
  8. }   

 二、註釋/JavaDoc

1.頂部版權聲明
2.包和引入塊(每一塊以空白行分隔)
3.類或接口的聲明。 在Javadoc註釋,描述的類或接口的用途。 

  1. /*  
  2.  * Copyright (C) 2007 The Android Open Source Project  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  *      http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */   
  16. package com.android.internal.foo;   
  17. import android.os.Blah;   
  18. import android.view.Yada;   
  19. import java.sql.ResultSet;   
  20. import java.sql.SQLException;   
  21. /**  
  22.  * Does X and Y and provides an abstraction for Z.  
  23.  */   
  24. public class Foo {   
  25.     ...   
  26. }   

類/接口註釋的內容 (1項 要求寫上)
類、接口的文檔註釋包含如下信息:
1.用途。 開發人員使用某個類/接口之前,需要知道採用該類/接口的用途。
2.如何使用。開發人員需要知道該類/接口應該如何使用,如果必要的話還需要註明不應
該如何使用。
3.開發維護的日誌。一個有關於該類/接口的維護記錄:時間、作者、摘要。

方法註釋的內容 (1,5,6,7項 要求寫上)
1.類該方法是做什麼的 。
2.該方法如何工作。
3.代碼修改歷史紀錄。
4.方法調用代碼示範。
5.必須傳入什麼樣的參數給這個方法。@param
6.異常處理。@throws
7.這個方法返回什麼。@return

三、在Imports使用通配符的優劣

四、局部變量應該推遲至使用前聲明並初始化

五、域(Field)命名

* 非公有,非靜態字段命名以m開頭。 
* 靜態域命名以s開頭。
* 其他字段以小寫字母開頭。
* public static final 字段(常量) 全部大寫,並用下劃線連起來。

  1. public class MyClass {   
  2.     public static final int SOME_CONSTANT = 42;   
  3.     public int publicField;   
  4.     private static MyClass sSingleton;   
  5.     int mPackagePrivate;   
  6.     private int mPrivate;   
  7.     protected int mProtected;   
  8. }   

六、花括號沒有獨自一行,它們與它前面的代碼佔同一行

七、命名規則

1.包 小寫。

com.chinacache.billing  
com.chinacache.billing.node 

2.類   大小寫字母混合組成,頭字母大寫。

class Raster;  
class ImageSprite; 

3.接口 大小寫字母混合組成,頭字母大寫,常以"able"、"ible"結尾 。

interface RasterDelegate;
interface Runna ble ;
interface Accessible ; 

4.方法 大小寫字母混合組成,第一個單詞的首字母小寫,其後單詞的首字母大寫。

run();
runFast();
getBackground(); 

5.變量、參數 小寫 ,不推薦使用下劃線 ,簡短明晰。

char c;
int i;
float m yW idth; 

6.集合、數組 應該從命名中體現其複數的含義,例如加後綴s或前綴some。

customers ;
postedMessages ;
some Customers ;
some Items ;

八、在定義類時,應該按照訪問權限的大小分別排列屬性和方法。

1. public
2. protected
3. 包級別(沒有訪問修飾符的,默認爲friendly)
4. private

 


寫好代碼以後對照代碼規範一項一項檢查一下吧。

(1)Eclipse 代碼格式化
你可以導入development/ide/eclipse下的文件,使得Eclipse按照Android代碼風格規則。選擇 “Window › Preferences › Java › Code Style,使用 “Formatter › Import” ,導入android-formatting.xml,”Organize Imports › Import” 導入 android.importorder.


(2)eclipse tab 設置爲4個空格:
Preferences -> General -> Editors -> Text Editors:
Insert spaces for tabs

(3)自動格式化代碼 Ctrl+Shift+ F

(4)全局 查找並替換 Ctrl+F

 

[參考] 
http://source.android.com/source/code-style.html
http://wenku.baidu.com/view/ce17addd5022aaea998f0fad

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章