Android/Java代碼規範__Java

來源:互聯網
上載者:User

由於公司最近的開發人員不斷增多,作為一個公司的開發負責人,必須要做到代碼可控性,總結了一下之前的開發經驗,設計了一套開發代碼規範,原本是給予內部使用的。現在這裡想代價分享一下。

公司內部的規範主要是使用了checkstyle、findbugs + jenkins 來做控制。

跑完之後,才發現自己寫的代碼各式各樣令人堪憂。

具體規範內容如下: Android/Java代碼規範 一、文檔曆史

版本 說明 日期 作者
1.0 Java代碼規範 Draft 2014-7-1 周聖韜
2.0 誠壹Java規範制定 2016-7-25 周聖韜
二、簡介

好的代碼規範能減小溝通成本、重構成本與維護成本,降低bug出現機率。
本文闡述了誠壹金融Java項目與Android項目使用的Java代碼規範。

說明

“DO” 為必須遵守。 2.1自動化格式的環境設定

使用Eclipse或者idea環境進行開發,需要匯入一下自動格式化設定檔。與Checkstyle外掛程式設定檔。 2.1.1

格式化
Eclipse的自動化格式環境目錄設定,使用以下檔案。 checkstyle_config_chengyiwm.xml eclipse_code_formatter.xml eclipse_code_templates_chengyi_java.xml 2.2自動檢查外掛程式 Checkstyle:http://eclipse-cs.sourceforge.net/update Findbugs:http://findbugs.cs.umd.edu/eclipse

http://chengyiwm-wordpress.stor.sinaapp.com/uploads/2016/08/ci.zip‘>點擊下載:代碼格式化、checkstyle設定檔 Android Java代碼規範 三、文檔格式

此文檔格式與注釋等的要求,大部分可以通過Eclipse的自動化格式完成 3.1排版規範 源檔案編碼 DO 1.所有源檔案編碼均為UTF-8 DO 2.縮排為4個space,(不使用tab重要) DO 3.每行不超過200個字元,超過需要折行 空格、空行

左右的變數,操作符,參數之間必須加入空格

Bad Example:(操作符’if’、’=’的前後沒有空格)

        Profile profile=getUserInfo(param.getReqHead().getUserId());        GetUserInfoResp resp=new GetUserInfoResp();        if(profile!=null){            BeanUtils.copyProperties(profile, resp);        }        resp.setUserType(authInfoService.getAuthStatus(param.getReqHead().getUserId()));        return resp;

Bad Example:(花括弧沒有換行)

        if(profile!=null){BeanUtils.copyProperties(profile, resp);}
屬性順序 DO 4.方法的屬性順序為public、protected、private 大括弧 DO 5.起始大括弧不要單獨佔一行,結束的大括弧獨立成行

Bad Example:

if (condition){//do something}

正確:

if (condition) {    //do something}
DO 6.若代碼塊內只有一行語句,則也不能省略大括弧。如:

Bad Example:

if (gpsFightInfo == null)showLoadingScreen(SCREEN_ACTION_RESUME, 0, 1.0f);elseshowLoadingScreen(SCREEN_ACTION_RESUME, 0, 0.5f);for (int i = 0; i < headers.length; i++)headers[i] = headers[i].toLowerCase();

正確:

if (gpsFightInfo == null) {showLoadingScreen(SCREEN_ACTION_RESUME, 0, 1.0f);} else {showLoadingScreen(SCREEN_ACTION_RESUME, 0, 0.5f);}           for (int i = 0; i < headers.length; i++) {headers[i] = headers[i].toLowerCase();}
3.2注釋 DO 7.使用標準javadoc(CTRL+ALT+J)使用template
Example:
    /**     * 新增地址     * @param addressparam地址參數     * @return 地址請求返回結果     * @throws IOException io讀寫錯誤     */    public Object addAddress(AddAddressParam addressparam) throws IOException {        return addressService.addAddress(param);    }

Bad Example: (不要使用無意義的自動以註解@Description,@param @param param 不知道什麼意思….)

    /**     * @throws IOException      * @throws NoSuchAlgorithmException      * @throws KeyManagementException      *      * @Description:使用者註冊     * @author wangshuxiong@chengyiwm.com     * @param @param param      * @param @return     * @return Object     * @date 2016年4月21日 下午1:12:28     * @throws     */    public Object updateAddress(UpdateAddressParam param) throws KeyManagementException, NoSuchAlgorithmException, IOException
DO 8.public的/class/interface/enum需要注釋 DO 9.public的field/method需要注釋

必須寫在上面,不能換行

Bad Example:

    private Integer opType;// 0 建立 1修改

Bad Example 2:

    // 0 建立 1修改    private Integer opType;

正確:

    /** 操作狀態 0 建立 1修改 */    private Integer opType;
四、命名

不要小看命名,不合適的命名會帶來很大的閱讀障礙,也加大後期的重構與維護成本。 4.1基礎命名規範 DO 10.Package\Class的命名為lowercase,且不帶短線和底線

Example:

com.chengyiwm.goldman
DO 11.class/interface/enum的命名使用PascalCase

Example:

AssetManagerScreenScreenType
DO 12.method的命名使用camelCase

Example:

public void loadEssentialAssets();
DO 13.private 或者protected field使用“m”首碼(m的含義為member),底線後的部分使用camelCase

Example:

private AssetManager mAssetManager;
DO 14.static field使用g首碼(g的含義為global),底線後的部分使用camelCase

Example:

private static Class[] gClassArray;
DO 15.constants的命名為FULL_UPPER_CASE,且用底線分割單詞

Example:

public static final int ACCESS_RANDOM = 1;public static final String FONT_HP_PATH = "fonts/hp.fnt";
DO 16.enum中各欄位的命名為PascalCase,且每個欄位獨立成行
Example:
public enum Element {Earth,Hero,    WaterFire}
建議:命名必須是自解釋的、有意義的,不要用i、j、k等名字命名

Bad Example:
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}

命名必須使用合理、常用的英文單詞,或行業術語,杜絕用冷門詞彙 DO 17.縮寫的使用必須參考縮寫規範表,不在縮寫規範表裡的不許用 DO 18.Method名字需要是doSomething DO 19.Class名字需要是NamePhrase(首字母大寫) DO 20.資料類型不需要加首碼、匈牙利命名法 常用資源命名 DO 21.Activity命名:xxxxActivity DO 22.View命名:xxxxView 或者 xxxxLayout DO 23.Service命名:xxxxService DO 24.BroadcastReceiver命名:xxxxReceiver DO 25.Layout檔案命名:
Activity的Content View應使用:activity_xxxx.xml 的形式
自訂View應使用:view_xxxx.xml 資源檔命名 DO 26.關於StateListDrawable各種狀態的命名尾碼

檔案名稱全部小寫,寫明按鈕的具體作用和狀態,單詞之間使用底線串連。

Bad Example:

Click_button.pngclickButton.png….

正確:

    login_normal.png    login_pressed.png    login_focused.png    xxxx_checked.png    xxxx_focused.png    xxxx_selected.png    xxxx_pressed.png    xxxx_disabled.png    xxxx_normal.png
DO 27.常量檔案的命名
strings.xml//所有的字串資源檔需要在此聲明dimens.xmldrawables.xmlstyles,xml//通用的Style樣式需要再次寫themes.xmlcolors.xmlids.xml
DO 28.第三方外掛程式,則需要加一個首碼,與本地資源區分開
Example:
正確:
學霸外掛程式logo:xueba_logo.png玩圖外掛程式logo: wantu_logo.png錯誤:學霸外掛程式logo:logo.png玩圖外掛程式logo: logo.png

/*
* @author zhoushengtao(周聖韜)
* @since 2016年7月16日 下午16:47:20
* @weixin stchou_zst
* @blog http://blog.csdn.net/yzzst
/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.