一、異常
1.不要忽視異常處理
如果像下面的代碼這樣,對catch後的異常作空處理,就像埋下地雷一樣讓人感覺到毛骨悚然:
錯誤的做法:
- void setServerPort(String value) {
- try {
- serverPort = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- }
- }
正確的做法(1):
在方法聲明時拋出異常,由客戶程序員去負責消化這個異常。
- void setServerPort(String value) throws NumberFormatException {
- serverPort = Integer.parseInt(value);
- }
正確的做法(2):
- /** Set port. If value is not a valid number, 80 is substituted. */
- void setServerPort(String value) {
- try {
- serverPort = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- serverPort = 80; // default port for server
- }
正確的做法(3):
- /** Set port. If value is not a valid number, die. */
- void setServerPort(String value) {
- try {
- serverPort = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- throw new RuntimeException("port " + value " is invalid, ", e);
- }
正確的做法(4):
- void setServerPort(String value) throws ConfigurationException {
- try {
- serverPort = Integer.parseInt(value);
- } catch (NumberFormatException e) {
- throw new ConfigurationException("Port " + value + " is not valid.");
- }
- }
2、不要偷懶而捕捉一般異常
下面代碼一概捕捉Exception異常,大小通吃是不對的,這樣會讓你在錯誤出現時難以定位到錯誤原因,一般異常無法用統一方法進行異常處理。
錯誤的做法:
- try {
- someComplicatedIOFunction(); // may throw IOException
- someComplicatedParsingFunction(); // may throw ParsingException
- someComplicatedSecurityFunction(); // may throw SecurityException
- // phew, made it all the way
- } catch (Exception e) { // I'll just catch all exceptions
- handleError(); // with one generic handler!
- }
二、註釋/JavaDoc
1.頂部版權聲明
2.包和引入塊(每一塊以空白行分隔)
3.類或接口的聲明。 在Javadoc註釋,描述的類或接口的用途。
- /*
- * Copyright (C) 2007 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.android.internal.foo;
- import android.os.Blah;
- import android.view.Yada;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- /**
- * Does X and Y and provides an abstraction for Z.
- */
- public class Foo {
- ...
- }
類/接口註釋的內容 (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 字段(常量) 全部大寫,並用下劃線連起來。
- public class MyClass {
- public static final int SOME_CONSTANT = 42;
- public int publicField;
- private static MyClass sSingleton;
- int mPackagePrivate;
- private int mPrivate;
- protected int mProtected;
- }
六、花括號沒有獨自一行,它們與它前面的代碼佔同一行
七、命名規則
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