Develop naming rules

Source: Internet
Author: User

Develop naming rules

Identifier name Method
There are four types of identifiers:

1 Camel naming method: Also known as Camel naming method, the first letter of all other words except the first word is capitalized.

2 Pa pascal (pascal) naming method: Also known as the big camper naming method, the first letter of all words in uppercase

3. Underline naming: Separate words with underscores.

4 Hungary naming: This method is widely used in Microsoft programming environments. The variable type is described by appending a lower-case sequence before the variable in Pascal naming. The naming method of quantity is: <scope _> + <prefix _> + <qualifier> range prefix, type prefix, and qualified word.

I personally think that the identifier naming principle: use as few characters as possible to fully express the meaning of the identifier.

1 short words can be abbreviated by removing the "vowel"
2. The first few letters of a long word can be abbreviated.
3. There are also some common abbreviations of English words.

The following are common abbreviations:


Ic (mainly used in app icons)
Cl (mainly used for color values)
Di (mainly used for separation line, including not only divider in Listview, but also line in normal layout)
Sl (mainly used for multiple States of a view, including not only selector in Listview, but also selector of buttons)
Bg (mainly used for la S and sub-la S)
Wnd (win)

The abbreviation principle is used in the program: Do not use the abbreviation unless it is customary.

Naming rules:
Packages: Uses anti-Domain Name naming rules and uses lowercase letters. The first-level package name is com, the second-level package name is xx (can be a company or an individual's casual), the third-level package name is based on the application name, and the fourth-level package name is module name or level name.

Package name
This package contains
Com. xx. Application name abbreviation. activities

Activity Class used on the page (activities level Name User Interface Layer)
Com. xx. Application name abbreviation. base
Each Activity class on the page can be written as an I-amount BaseActivity class (basic shared class)

Com. xx. Application name abbreviation. adapter
Page Adapter class (Adapter class)

Com. xx. Application name abbreviation. tools
This package includes: Public tool method class (tool module name)

Com. xx. Application name abbreviation. bean
(Or com. xx. Application name abbreviation. unity)
This package includes: Element class

Com. xx. Application name abbreviation. db
Database Operations

Com. xx. Application name abbreviation. view

(Or com. xx. Application name abbreviation. ui)
Custom View class

Com. xx. Application name abbreviation. service

Com. xx. Application name abbreviation. broadcast
Broadcast Service
Class 2 (classes): a noun that uses the big camper method to avoid abbreviations unless the abbreviation is well known, such as HTML and URL. If the class name contains a word abbreviation, each letter of the abbreviation should be capitalized.
For example
Activity Type
Aty or Activity is a suffix
Welcome Page class WelcomeAty. Or WelcomeActivity

Adapter class
Adp or Adapte is a suffix
News details adapter NewtDetailAdp or NewDetailAdapter

Resolution class
Hlr is a suffix identifier
Common Methods
Tools or Manager is a suffix

Thread Pool Management class: ThreadPoolManager
Log tool: LogTools

Marked with the DBHelper suffix

News Database: NewDBHelper

Marked with a Service extension
Time Service TimeService

BroadcastReceive class: identifies the time notification TimeBroadcast with the Broadcast suffix
ContentProvider: identified by the Provider suffix
Shared basic classes written directly: BaseActivity and BaseFragment starting with Base

3 interface: Like a class, the naming rule uses the big camper method and ends with able or visible, such as interface Runna ble;
Interface Accessible.

Method 4 (methods): a verb or a noun. The method of using a small camper is called onCreate () and run ()

InitXX (): Initialize the related method and use init as the prefix to identify the layout, such as initView ()

IsXX (): If the return value of the checkXX () method is boolean, use is or check as the prefix.

GetXX (): The method for returning a value. It is identified by the get prefix.

ProcessXX (): indicates the method used to process data. process is identified as a prefix whenever possible.

DisplayXX (): a prompt box and prompt information are displayed, marked with the prefix "display ".

SaveXX (): Id related to saving data, identified by the sav prefix e

ResetXX (): indicates the reset prefix used for data restructuring.

ClearXX (): clears data-related

RemoveXXX (): clears data-related

DrawXXX (): Used to plot data or effects. It is identified by the draw prefix.


Variables use the small camper name method. The control name must be consistent with the xml layout id.

By placing a keyword at the end, you can create more unified variables, which are easier to understand and search. For example, use strCustomerFirst and strCustomerLast instead of strFirstCustomer and strLastCustomer.
Quantifiers: Description of the quantifiers suffix
The First variable in the First group
The Last variable in the Last group
Next variable in a group of Variables
The previous variable in the Prev Group
The current variable in the Cur Group
6 Constants (Constants) are all capitalized and the underline naming method is used. For example: MIN_WIDTH

7. resource files (in the drawable folder of images): All the files are in lower case and are separated by prefix using the underline naming method.

Naming mode: activity name_logical name/common _ logical name

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


Btn_xx: Use btn _ overall effect (selector) for button images)

Btn_xx_normal: the button image uses btn _ normal effect

Btn_xx_press: The effect when btn _ is clicked on the button Image

Bg_head: Use bg _ to describe the background image

Def_search_cell: default image def _ function _ description

Icon_more_help: icon image use icon _ function _ description

Seg_list_line: Use the seg _ function for images with separated features _ description

Sel_ OK: select the icon and use sel _ function _ description

Name suffix:
Suffix description
Unit: This suffix is used for element images when the xml tilemode is used for image configuration.
Nor: The image status, indicating the normal status.
Hl: the status of the image, indicating the highlighted status.
Press: The image status, indicating the press status
Select: the status of the image, indicating that the view it occupies is selected
Unselect: the status of the image, indicating that the view it occupies is not selected

8. Resource layout file (XML file (layout file )):
All lowercase letters, with the underline name

1). contentview name. The default layout of the Activity is used to name the Activity class with no suffix removed. Without a suffix:
Function module. xml
For example, main. xml, more. xml, and settings. xml
Or activity _ function module. xml
Example: activity_main.xml and activity_more.xml
2). Dialog name: dialog _ description. xml
Example: dlg_hint.xml
2). PopupWindow name: ppw _ description. xml
For example, ppw _ info. xml
3). Name the list item listitem _ description. xml
Example: listitem_city.xml
4). included items: include _ module. xml
For example, include_head.xml and include_bottom.xml
5). Sub-layout of the adapter: function module _ item. xml
For example, main_item.xml,

9. animation files (under the anim folder): All lowercase letters are separated by prefix by using the underline naming method.
// The front is the animation type and the back is the direction
Animation naming example
Standard writing remarks

Fade_in: fade in
Fade_out: fade out
Push_down_in: push from below
Push_down_out: available below
Push_left: Push image left
Slide_in_from_top: slide from the head
Zoom_enter: enters the Deformation
Slide_in: Slide
Shrink_to_middle: narrowing down in the middle

10 Resource ID (resourcesid): The Case sensitivity specification is consistent with the method name, and the method name is small. The naming convention is "resource control abbreviation" + "variable name ". Note: The Page Control name should be consistent with the Control id name.
Name the IDs in strings. xml and colors. xml:
Naming mode: activity name_function module name_logic name/activity name_logic name/common _ logic name
In strings. xml, use activity name annotations to differentiate the file content.
11 name the id in layout
Naming mode: view abbreviation _ module name_view logical name
The abbreviation of view is as follows:

Control abbreviation

LayoutView lv
RelativeView rv
TextView TV
Button btn
ImageButton imgBtn
ImageView mgView or iv
CheckBox chk
RadioButton rdoBtn
AnalogClock anaClk
DigtalClock dgtClk
DatePicker dtPk
EditText edtTxt
TimePicker tmPk
ToggleButton tglBtn
ProgressBar proBar
SeekBar skBar
AutoCompleteTextView autoTxt
ZoomControls zmCtl
VideoView vdoVi
WdbView webVi
RantingBar ratBar
Spinner SPNs
Chronometer cmt
ScollView sclVi
TextSwitch txtSwt
ImageSwitch imgSwt
ListView lVi or lv
ExpandableList epdLt
MapView mapVi

12. Name the view variable in the activity

Naming mode: Logical name + view abbreviation

Suggestion: If the layout file is complex, we recommend that you divide layout into multiple modules. Each module defines a moduleViewHolder whose member variables contain the view

13. styles. xml: extract the styles that are repeatedly reproduced in layout into general style components and put them in styles. xml;

14. Use layer-list and selector


Android coding standard suggestions (some people think it makes sense)

1. the java code does not contain Chinese characters. A maximum of Chinese characters can appear in comments.

2. Local variable naming and static member variable naming

Only letters are allowed. The first letter of a word is in uppercase, And the other letters are in lowercase.

3. Constant naming

It can only contain letters and _. All letters are in uppercase. Words are separated _.

4. Split images into multiple reusable images as much as possible

5. What the server can achieve is not to be placed on the client.

6. Exercise caution when referencing a third-party library. Avoid using a large third-party library, resulting in a large client package.

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

8. process image. 9

9. Exercise caution when using static variables to share Interfaces

10. Log (system name module Name Interface Name, detailed description)

11. unit test (logical test and Interface Test)

12. Do not reuse the handler of the parent class. The handler of the corresponding class should not be used by its subclass. Otherwise, the message. what conflict will occur.

13. process all logic in a View. OnClickListener in the activity

14. Use % 1 $ s in strings. xml for character string wildcard

15. If multiple activities contain common UI processing, you can extract a CommonActivity and call the common part for processing. Other activities only need to inherit it.

16. When you use button + activitgroup to implement the tab effect, use Button. setSelected (true) to ensure that the button is in the selected state and make the current activity of activitygroup correspond to the button

17. If a common component is developed, add a prefix to the file name in the drawable/layout/menu/values directory to avoid conflict.

18. data must be validated, such

The numeric type must have a default value if the conversion fails;

Whether the server response data is valid

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: 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.