Best Practices for Android code specification

Source: Internet
Author: User

Naming specification Package naming conventions

The use of anti-domain naming rules , package name all lowercase, continuous words are simply connected, not using underscores, a level package named COM, level two package named XXX (can be a company domain name or personal name), three-level package name according to the application, four-level package named Module name or hierarchy name. Such as:

com.isa.crm.activity|
com.isa.crm.adapter

Java class naming conventions

Use the large hump-type nomenclature to avoid abbreviations, unless the abbreviation is well known, such as Html,url, if the class name contains a word abbreviation, each letter of the acronym should be capitalized. Such as:

Product| ProductManager |
ProductListActivity| ProductListAdapter |JsonHTTPSRequest

Interface Naming conventions

The naming convention, like the class, uses a large hump-named method , with able or ible ending. For example:

interface Runable|interface Accessible

Member Variable naming conventions

Using the Small hump naming method .

Temporary variable naming

Using the standard Java naming method, Google's M-naming method is not recommended. For example:

private String userName;It is not recommended to useprivate String mUserName;

Constant naming

Constants are named using all caps and underscores. For example:

public static final String TAG = "tag";

Control instance naming

The name of the control in the class must be consistent with the XML layout ID (can be removed {module_name} ). For example:

The ID of the Button in the layout file is:android:id="@+id/btn_pay"

private Button btn_pay;

Method Naming conventions

Verbs or verb nouns, using the Small Hump name Method . For example:

run();| onCreate(); |syncProducts();

Layout file ( Layout) Naming conventions

All lowercase, using the underscore name method. It {module_name} is a modular name or abbreviation for a business module or a function module.

Activity Layout: {module_name}_activity_{名称} For example:

crm_activity_main.xml|crm_activity_shopping.xml

Fragment layout: {module_name}_fragment_{名称} For example:

crm_fragment_main.xml|crm_fragment_shopping.xml

Dialog layout: {module_name}_dialog_{名称} For example:

crm_dialog_loading.xml

list item layout naming : {module_name}_list_item_{名称} For example:

crm_listitem_customer.xml

include item layout naming : include_{名称} For example:

include_head.xml

Adapter Sub-layouts : {module_name}_item_{名称} For example:

qz_item_order.xml

Widget layout: {module_name}_widget_{名称} For example:

crm_widget_shopping_detail.xml

Resource ID naming specification

The naming pattern is: {view缩写}_{module_name}_{view的逻辑名称} , such as:

Customer Management CRM module layout layout LinearLayout id–>ll_crm_content

module abbreviation for the ImageView layout of QZ id–>iv_qz_photo

The common control view and its abbreviated reference table are as follows:

Picture resource file naming specification

icon Name : {module_name}_ic_{名称} For example:

crm_ic_app.png

background image naming : {module_name}_bg_{名称} For example:

crm_bg_navbar_highlight_normal.9.png

button named : {module_name}_btn_{名称} for example:

crm_btn_login_normal.9.png

button checkbox picture naming : {module_name}_checkbox_{名称} For example:

crm_checkbox_cart_true.png

Other image names : {module_name}_icon_{名称} For example:

qz_icon_blue_circle.png

Code style curly braces problem

Style One

if (hasMoney()){}else{}

Style Two

if (hasMoney()) {} else {}
Whitespace issues

if else| while | 运算符两端 Wait for the following space to be separated. For example:

How the specification is written:

if (hasMoney()) {} else {}for (int i = 0; i < 10; i++) {}

Non-standard writing method:

if(hasMoney()){}else{}for(int i=0; i<10;i++){}
Method parameters

Line wrapping is required when the number of method parameters is too large.

Comments

You must comment on all instance variables, class constants, for example:

// 用户姓名private String userName

all classes and interfaces must be commented on for example:

/*** Activity基类** @author [email protected]*/public class BaseActivity extends Activity{}

All methods must be commented on for example:

/*** 请求** @param path 路径* @param generalParams 基本参数* @param businessParams 业务参数* @return 请求结果* @throws ApiException 请求错误则返回该异常*/public Map<String, Object> request (String path,              Map<String, Object> generalParams,              Map<String, Object> businessParams) throws ApiException {   return null;}

More details can also be consulted:
[Google Java编程风格指南](http://hawstein.com/posts/google-java-style.html)

Hump-type nomenclature (CamelCase)
    • Large Hump-type nomenclature ( UpperCamelCase ):
      The first letter of each word is capitalized as:XmlHttpRequest

    • Small Hump-type nomenclature ( lowerCamelCase ):
      In addition to the first word, the first letter of each word is capitalized as follows:xmlHttpRequest

Description

This article describes some of the common naming conventions used in the development of Android projects | Code writing style specification, which is derived from personal data collation (refer to Network technology blog), personal project practice. Reference to these specifications to facilitate the collaborative development of projects, project code of the unified style, in the post-project maintenance of more convenient and quick to find, understand and modify other people's code. If friends have better standard requirements, welcome to share and discuss together.



A list of the reference articles in this article:

    • Google Java Programming Style guide shared by Hawstein

    • Wizards share Android Code of research


If you have any questions about the content of the article or have a better opinion, please contact me by message or email:
[Email protected]

If you need to reprint, please indicate the source, thank you!!

Best Practices for Android code specification

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.