Android进阶之路——安卓编程规范

JUST DO IT

Posted by nobe on August 2, 2016

Android进阶之路——安卓编程规范

对于刚入行或者入行几年的程序员,或多或少,都有一点代码规范的问题,有些是没法子,各种需求变更,或入手各种别人写了一半的代码等等,因为时间问题,有时候你是心有余而力不足啊。虽然如此,我们还是得把代码规范养成一种习惯,这不仅仅是写出优美的代码,好的代码还能提高效率和提升性能的

一、包

包名全部采用小写,不用下划线区分单词

主包名采用[公司性质].[公司名称].[项目名称]的命名方式 例如:寻医问药医脉app com.xywy.askforexpert.

通用功能子包名采用[主包名].[通用名称]的命名方式 常见通用功能如下表:

包.png 一般功能子包名采用[主包名].[模块名称].[子模块名称]的命名方式,例如: 手机助手的闹钟模块包名 edu.feicui.assistant.alarm

只需导入用到的类,不得用*导入包下所有类

导入类时,系统类在上方,自定义类在下方

二、代码

代码主要采用大/小驼峰命名法,即除首字母外,每个单词首字母大写,整体首字母大小根据其它规范决定

类名、接口名、枚举名等首字母大写,若由多个单词组成,则其后每个单词首字母大写,例如: class ConfigManager{}

类名、接口名、枚举名等首字母大写,若由多个单词组成,则其后每个单词首字母大写,例如: class ConfigManager{}

继承自安卓组件的类,采用父类名作为后缀,例如: class LoginActivity extends Activity{}

自定义异常必须以Exception结尾

除for循环变量外,一律不得使用i、j、k等单字符作为变量名

定义数组时方括号紧随在原始类型之后,数组名称一般使用复数形式,例如:

int[] arrays;

常量、枚举等均采用大写形式,用下划线区分各单词,例如:

final static int DIALOG_ID_ALARM = 1;
enum Season{SPRING, SUMMER, AUTUMN, WINTER};

全局变量添加所有者前缀:实例成员变量前缀m(表示member),类静态变量前缀s(表示static),例如: 实例变量mRun 类静态变量sInstance

控件变量添加组件前缀,顺序在所有者前缀之后,例如: 全局名称mBtnNext 局部名称btnNext 常见控件前缀如下表:

空间前缀.jpg 除单例模式外一般不得使用静态变量

常量一般使用final static修饰,根据需要使用可见性修饰符,例如:

public static final int VISIBLE = 0x00000000;

一般方法名首字母小写,若由多个单词组成,则其后每个单词首字母大写

构造方法采用递增方式(参数多的写在后面) 例如:

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) {}

仅在项目内使用的实体类不使用JavaBean进行封装,直接将成员变量访问修饰符修改为非private,例如:

class User{public String name,pwd;}

实体类中固定值的成员变量可设置成final,并通过构造函数初始化

实体类中不得随意修改的成员变量可添加下划线前缀以作区别,例如:

class User{public int _id;}

一般不使用System.out输出,而是使用Log中的方法

使用BuildConfig.DEBUG标记对Log进行封装,只在调试时输出重要信息,正式版不输出

一般try……catch只捕获需要的异常

catch块不得为空,至少应当将异常信息输出

三、资源

资源命名全部采用小写,各单词间以下划线区分

布局文件采用[前缀]_[功能模块].xml的命名方式,例如: MainActivity的布局activity_main.xml 常见前缀如下表:

常见前缀.png 图片采用[性质前缀][功能模块] [属性后缀].[扩展名]的方式,例如: 主背景图片bg_main.png 确定按钮按下btn_ok_presssed.png 常见属性后缀如下表:

常见属性后缀.png values目录下文件名称较固定,不得随意更改,常见名称如下:

常见名称.png ### 四、注释 开源项目必须添加文件注释,非开源项目建议添加,例如:

     /*
      * @(#): 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.
      */
  ```
类定义一般需要写类注释,接口一般需要写接口注释,如果没有文件注释,则需要在类注释和接口注释中标出作者,例如:

/** * 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 {} ```

成员变量、静态变量、常量等添加属性注释,例如:

     /** This view is invisible. */
     public static final int INVISIBLE = 0x00000004;
     ```
关联性较大的多个成员变量等可以共用同一条注释,例如:

	```
     /** 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标出,例如: ```
 @Override
 protected void onCreate(Bundle savedInstanceState) {}
 ``` 不建议继续使用的方法用@Deprecated标出

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加以标记,例如:
 void write(byte[] buf, File file) {
 //TODO XXXX
    }

若功能已完成,但存在效率等潜在问题时,使用XXX加以标记,例如:


    void parseXML(File file) {
         // XXX: Maybe SAX is better
         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
         Document doc = builder.parse(file);
     }
     ```
若代码存在严重问题或仅用于调试,使用FIXME加以标记(注:存在FIXME标记的代码不能作为正式版发布)

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等代码块过长,可以在结尾处标记循环变量,例如:
    for (int position = 0; position < 10; position++) {
            ……
    }// end for: position
    while(mRun){
            ……
    }// end while: mRun 

持续更新中,欢迎继续补充!