Android Naming conventions

Source: Internet
Author: User
Tags naming convention

Android Naming Conventions

Just touch Android, the name is in accordance with pinyin, so sometimes want to understand the name of the control what is the use of, it is necessary to read and even several times to know, so that in the code review and modification process will waste a lot of unnecessary time. If I am a person to develop, a person to maintain the words are OK, but if a project is a team division of labor, so that your colleagues to see your code is even more difficult, because the programming method is different between us, so in the development process, the naming standard unification is particularly important, preferably the team in a unified good name method, This will be a lot easier for future work.

In the interview, review a programmer's programming level, naming norms is also a big standard, so, below I summarize from the experience of others on the Internet and their own understanding of the collection summary of the Android naming norms, I hope to help you.

Reminder: Under the unification of naming conventions, add comments, so that future maintenance will be efficient and fast much.

First, introduce the current main identifier naming and abbreviation rules: (This part of the other side for reference)

Before we explain the naming specification, we introduce the main identifier naming method and English abbreviation rules at first.

Identifier naming method
There are four types of identifier naming methods:

1 Hump (Camel) Nomenclature: Also known as the small hump nomenclature, except for the first word, the first letter of all the remaining words capitalized.

2 Pascal (PASCAL) nomenclature: Also weigh hump nomenclature, the first letter of all words capitalized

3 underline naming: The words and the words are underlined between the intervals.

4 Hungarian nomenclature: Widely used in the Microsoft programming environment, the type of the variable is indicated by attaching a lowercase sequence before a variable that is named Pascal. The amount is named:<scope_> + <prefix_> + <qualifier> range prefix, type prefix, qualifier.

Personally think the identifier naming principle: As far as possible with the fewest characters and can fully express the meaning of identifiers.

English abbreviation principle:
1 shorter words can be abbreviated by removing "vowels"
2 longer words are desirable for the first few letters of a word to form abbreviations
3 In addition, there are some conventions idiomatic the English word abbreviation.

The following are common abbreviations for English words:

Naming conventions:

1 Pack (Packages): Use the anti-domain naming rules, all using lowercase letters. The first level package name is COM, level two package name is XX (can be company or personal casual), three level package name according to the application to name, four level package named Module name or hierarchy name

Class 2 (classes): Nouns, using the big hump nomenclature, try 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.

3 Interface (interface): Naming rules and classes using the big hump nomenclature, more able or ible end, such as interface Runna ble;
Interface Accessible.

4 Methods (methods): verbs or verb nouns, using small hump nomenclature such as: OnCreate (), run ()

The 5 variable (variables) uses the small hump name method. The name of the control in the class must be consistent with the XML layout ID.

By placing a quantifier at the end of a uniform quantifier, you can create more uniform variables that are easier to understand and easier to search. For example, use Strcustomerfirst and strcustomerlast instead of using Strfirstcustomer and Strlastcustomer.
quantifier list: quantifier suffix description
First a set of variables
Last of a set of variables
Next variable in a set of variables
Prev the previous in a set of variables
Cur the current variable in a set of variables

6 Constants (Constants) are all capitalized and are underlined. For example: Min_width

7 resource file (Picture drawable folder): All lowercase, using the underscore name, plus the prefix to distinguish

Naming pattern: Activity Name _ logical name/common_ logical name

If there are multiple forms such as buttons, etc. except as btn_xx.xml (selector)

Named suffix:

8 Resource Layout file (XML file (layout file)):

All lowercase, using the underscore name method

1). Contentview named, activity default layout, to remove the suffix of the activity class to name. Do not add suffix:

function module. xml

For example: Main.xml, More.xml, settings.xml

Or: Activity_ function module. xml

For example: Activity_main.xml, Activity_more.xml

2). Dialog name: Dialog_ description. xml

Example: Dlg_hint.xml

2). Popupwindow Name: ppw_ description. xml

Example: PPW _info.xml

3). List item name listitem_ description. xml

Example: Listitem_city.xml

4). Contains items: Include_ module. xml

For example: Include_head.xml, Include_bottom.xml

5). Adapter sub-layout: function Module _item.xml

For example: Main_item.xml,

9 Animation files (under the Anim folder): All lowercase, using the underscore naming method, plus the prefix distinction.

Front is the type of animation, followed by direction

10 Resource ID (RESOURCESID): The case specification is consistent with the method name, using the small hump naming method. The naming convention is "abbreviated name of resource control" + "variable name". Note: The page control name should be consistent with the control ID name

Name the ID in Strings.xml,colors.xml, etc.:

Naming pattern: Activity Name _ Function Module Name _ Logical name/activity name _ logical name/common_ logical name

Strings.xml, use activity name comments to separate the contents of the file

ID naming in one layout

Named mode: The logical name of the view abbreviation _ Module name _view

The details of view abbreviations are as follows:

Name of view variable in 12.activity

Named mode: logical name +view abbreviation

Recommendation: If the layout file is complex, it is recommended that layout be divided into modules, each of which defines a moduleviewholder whose member variables contain the view

13.styles.xml: The continuous style of layout is extracted from the common style generic components, put into the styles.xml;

14. Using Layer-list and Selector

Android Coding Specification Recommendations (others find it quite reasonable)

Chinese is not present in 1.java code, the maximum number of comments can appear in Chinese

2. Local variable naming, static member variable naming

Can only contain letters, the first letter of the word is uppercase, all the other letters are lowercase

3. Constant naming

Can only contain letters and _, all uppercase letters, between words separated by _

4. Image as many reusable pictures as possible

5. The server can be implemented, do not put on the client

6. Refer to third-party libraries be cautious, avoid the application of large-capacity third-party libraries, resulting in a very large client package

7. Handle the application of global exceptions and errors and send errors to the server by mail

8.9 Processing of pictures.

9. Use static variable mode to realize the sharing between interfaces be careful

10.Log (System name Module name Interface name, detailed description)

11. Unit Test (logic test, Interface test)

12. Do not reuse the handler of the parent class, the handler of a class should not be used for its subclasses, or it will cause message.what conflicts

Handle all logic in a view.onclicklistener in 13.activity

Using%1 $ s in 14.strings.xml to implement string wildcard

15. If more than one activity contains common UI processing, you can refine a commonactivity, call the generic part to handle it, and other activity just inherit it

16. When using Button+activitgroup to implement the tab effect, use button.setselected (true) to ensure that the button is selected and that the current activity of activitygroup corresponds

17. If you are developing a generic component, to avoid conflicts, add a prefix to the file name in the Drawable/layout/menu/values directory

18. Data must be tested, such as

The character type turns the numeral type, if the conversion fails must have the default value;

Whether the service-side response data is effectively judged

Finally, these are some personal summaries and suggestions from other netizens on the Internet,

For reference only, as long as a unity on the formation of a group can be simple to know what he means.

Programmers develop good coding habits and naming conventions that are good for themselves and for the entire team.

(Turn to self-love programming network http://www.icoding.net/android-named-rules.html)

Android Naming conventions

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.