Android development naming conventions and coding specifications

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/crazy1235/article/details/51346027

No rules inadequate surrounding area, is it? haha ~ ~

Fortunately, I just learned Java programming, the teacher instilled in the code of the relevant knowledge, and has been adhering to.

People who have experienced team development know that without a certain specification, the code will look miserable and even messy.

The following is the introduction of my personal coding process used in the specification for your reference ~ ~

Naming conventions
命名规范要望文知义,简单明了。命名规范定制太多,就会让人心烦,反而没人遵守了。                         ---《APP研发录》

Two naming conventions are introduced first:

    • Hump Name Method : Also known as the small Hump name method. Except for the first letter lowercase, all the remaining words are capitalized.

    • Pascal's nomenclature : The weigh of the hump name method. Capitalize all words in the first letter.

Naming conventions for packages
包名一律小写

The following rules are recommended: "com". "Company name/Organization name". "Project Name". "Module Name"

For example: Com.jacksen.mvp.demo. This directory is then layered according to the business logic.

A common package hierarchy is as follows:

    • Com.xxx.xxx.view–> a custom view or view interface

    • Com.xxx.xxx.activities–> Activity Class

    • Com.xxx.xxx.fragments–> Fragment Class

    • Com.xxx.xxx.adapter–> Adapter Related

    • Com.xxx.xxx.utils–> Public Tools Class

    • Com.xxx.xxx.bean–> entity class

    • Com.xxx.xxx.service–> Service

    • Com.xxx.xxx.broadcast–> Broadcast Receivers

    • Com.xxx.xxx.db–> database Operation class

    • Com.xxx.xxx.persenter–> Intermediate Object

    • Com.xxx.xxx.model–> Data Processing class

Naming conventions for classes

The naming of classes in Android is a consistent specification for Java development.

大驼峰命名法,即所有单词首字母大写。
    • Activity–> Xxxactivity.java

    • Application–> Xxxapplication.java

    • Fragment–> Xxxfragment.java

    • Service–> Xxxservice.java

    • Broadcastreceiver–> Xxxbroreceiver.java

    • Contentprovider–> Xxxprovider.java

    • Adapter–> Xxxadapter.java

    • Handler–> Xxxhandler.java

    • Interface –> Xxxinter.java

    • Interface implementation Class –> Xxximpl.java

    • Persenter–> Xxxpersenter.java

    • Public parent class –> Baseactivity.java, Basefragment.java,-Baseadapter.java, etc.

    • Util class –> Logutil.java

    • Database Class –> Basesqlitedbhelper.java

Naming conventions for variables
采用驼峰命名规则。

Java Common variables:

    • Resultstring

    • UserBean

    • Loginpresenter

Android Control variables:

    • Loginbtn

    • Inputpwdet

    • Shownametv

Some people recommend using the "control abbreviation" + "Control Logical Name" method, such as Btnlogin. But I am more accustomed to writing in turn, such as LOGINBTN. Similar to the name of the class, write the logical name in front.

Abbreviations for common controls

xxx_pbar
Control abbreviation in Layout file Abbreviations in code
linearlayout xxx_layout xxxllayout
relativelayout xxx_layout xxxrlayout
framelayout xxx_layout xxxfla Yout
TextView xxx_tv xxxtv
EditText Xxx_et xxxet
Button xxx_btn xxxbtn
I Mageview xxx_iv xxxIv
CheckBox xxx_chk xxxchk
RadioButton xxx_rbtn xxxrbtn
ProgressBar xxxpbar
ListView xxx_lv xxxlv
WebView XXX_WV Xxxwv
GridView xxx_gv xxxgv



Abbreviations for common words:

Word Abbreviations
Icon Ic
Background Bg
Foreground Fg
Initial Init
Information Info
Success Succ
Failure Fail
Error Err
Image Img
Library Lib
Message Msg
Password Pwd
Length Len
Buffer Buf
Position Pos



Constant Name:
All words are capitalized, separated by "_" between each word.

For example:

publicstaticfinal"http://apis.baidu.com/heweather/weather/free";
Naming conventions for methods
与java开发类似,采用驼峰命名规则。首单词首字母小写,其余单词首字母大写。尽量不要使用下划线。

Example:

    • Setxxx ()

    • GetXXX ()

    • Loginxxx ()

    • OnCreate ()

    • Ondestory ()

    • Isxxx () –> return value is a Boolean type

    • Checkxxx ()

Naming conventions for methods
全部采用小写,单词之间使用下划线分割。

Layout file:

    • Activity_login.xml

    • Fragment_first_tab.xml

    • Item_choose_city.xml

    • Dialog_choose_city.xml

    • Common_footer.xml

    • Popup_xxx.xml


Control ID:

The "abbreviations for common controls" table above basically lists the ID notation for commonly used controls.

    • Login_btn

    • Input_phone_et

    • Input_pwd_et

    • Login_pbar

Naming conventions under the Drawable directory
全部单词小写,单词之间采用下划线分割。
    • Icon –> ic_xxx.png–> ic_logo.png

    • Background map –> bg_xxx.jpg–> bg_splash.jpg

    • Selector–> Selector_login_btn.xml

    • Shape–> Shape_login_btn.xml

    • Picture status –> bg_login_btn_pressed.jpg &-Bg_login_btn_unpressed.jpg

Naming conventions under the Anim directory
单词全部小写,单词之间采用下划线分割。
    • Fade_in.xml

    • Fade_out.xml

    • Slide_in_from_left.xml

    • Slide_in_from_top.xml

    • Slide_out_to_right.xml

    • Slide_out_to_bottom.xml

Coding specifications
  • Try not to show Chinese in the code. Comments and exceptions. The code is displayed in Chinese using the Strings.xml reference.

  • The control declaration is placed at the activity level so that it can be used elsewhere in the activity.

  • Handle all the Click event logic in one view.onclicklistener, which looks very focused and intuitive.

  • The strings.xml uses theimplementation string, such as%1 s D, to implement a wildcard.

  • The font size in the layout file is defined in Dimens.xml.

  • The values for margin and padding are also placed in the dimens.xml.

  • Use the intent mode as much as possible between the interface values. Use less global variables.

  • Adding a click event to a layout file is not recommended.

  • Data type conversions must be verified.

  • Use constants instead of enumerations.

  • Entities are not shared between different modules, but can be shared across different pages under the unified module.

  • It is recommended that you write and format the code in the same line of code as the left parenthesis and the method name. It seems that the left parenthesis is in the form of C # in the next line.

  • Business is slightly more complex, it is possible to refine a baseactivity or basefragment out as a public parent class.

  • Class comments must be written, and the housekeeper's method must also write method comments. Constants write comments as much as possible.

The naming conventions and coding specifications in a project are a prerequisite for a project to be leader and an essential skill.

Set up a good specification, we must abide by, with a unified standard, the project is good maintenance, between each other just good review code, easy to develop and maintain.

Reference

"App Development Record"

Introduction is complete, welcome advice ~ ~

Android development naming conventions and coding specifications

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.