Android Product Development (i)--Practical development specification

Source: Internet
Author: User

From the beginning of this article we pause the analysis of the Android source code, began to talk about the Android product development in some commonly used techniques, techniques, methods, practices and other posture. What we need to emphasize here is that what we are explaining may be more commonly used in product development, because for project development, there may be more emphasis on management, the progress of things, more emphasis on engineering things, and we are more on the product of the technical aspects of the summary.

And this article chooses to develop the specification as the first article of this series, is the personal feeling product research and development process, the development norm really is very important, is very important, is very important (important thing to say three times), a good development criterion can let the person of the team to be more familiar with others ' code, the newcomer can better understand the business logic of the Development specifications are not a dead static, each team may have their own development specifications, as long as the appropriate team development specifications are the best development specifications.

So the development norms explained in this article can only be a point, there are desirable places to learn from, reference, can not copy the whole copy without thinking, after all, different teams have different actual situation. The best way to do this is to summarize the normative processes that are appropriate for your team according to the development specifications of this article.

Well, don't say much nonsense, let's introduce the Android development specification that I summarized in practice.

1 Preface 1.1 Why you need to develop specifications
编码规范对于程序员而言尤为重要,有以下几个原因:* 一个软件的生命周期中,80%的花费在于维护* 几乎没有任何一个软件,在其整个生命周期中,均由最初的开发人员来维护 * 编码规范可以改善软件的可读性,可以让程序员尽快而彻底地理解新的代码* 如果你将源码作为产品发布,就需要确任它是否被很好的打包并且清晰无误,一如你已构建的其它任何产品
1.2 The role of development specifications
* 减少维护花费* 提高可读性* 加快工作交接* 减少名字增生* 降低缺陷引入的机会
2 Naming conventions 2.1 constant naming conventions 2.1.1 types

Constant naming conventions

2.1.2 Description

Constants are used to save data that needs to reside in memory and often use less variation, and it is necessary to follow the principles of hope text to define the names of constants.

2.1.3 Rules
      1. all uppercase letters;
      1. The middle is connected with "_";
      1. The principle of knowing the meaning of the text;
2.1.4 Notes

When it comes to using values directly from a string or other primitive type in your code, it is recommended that you define constants to avoid using the same values as parameters directly in multiple places.

2.1.5 Example
    • For example, to define a constant that represents the minimum screen width, you can define a constant of type int, which can be named: "Min_screen_width";
    • Other examples:
    • For example: static final int min_screen_width = 4; (√)
    • For example: static final int min_screen_width = 4; (x)
    • For example: static final int minscreenwidth = 4; (x)
    • For example: static final int WIDTH = 4; (x)
    • For example: static final int width = 4; (x)
    • For example: static final int wd = 4; (x)
2.2 Variable naming specification 2.2.1 type

Variable Naming conventions

2.2.2 Description

Variables are used to save temporary data in the system, and the variables are named to follow the principles of hope text, simple and clear, and hump marking.

2.2.3 Rules
    1. Capitalize the first letter;
    2. Java hump naming;
    3. The principle of knowing the meaning of the text;
    4. The recommended reference type variable adds the prefix "M";
    5. If it is a view component variable, the component name is the ID name defined in the XML file with an underscore, followed by an underscore;
2.2.4 Notes

No

2.2.5 Example
    • For example, if you define a variable that represents the minimum screen width, you can define a temporary variable of type int: mminscreenwidth;
    • For example: static final int mminscreenwidth = 4; (√)
    • For example: static final int minWidth = 4; (x)
    • For example: static final int screenwidth = 4; (x)
    • For example: static final int width = 4; (x)
    • For example: static final int min = 4; (x)
    • For example: static final int MSW = 4; (x)
2.3 Method naming Specification 2.3.1 Type

Method Naming conventions

2.3.2 Description

The naming of method names should follow simple and clear principles;

2.3.3 Rules
    1. First letter lowercase;
    2. Java hump naming;
    3. Simple and clear principles;
    4. Initialize the View method init* (do one thing per init)
2.3.4 Notes
    • At the same time in the implementation of the method, try not to appear in a method too much implementation code, such as a method has hundreds of lines of the implementation of logic, recommended in logic complex, according to the function of the point of separation of multiple methods, easy to read.
    • In addition, the function of the same implementation logic, as far as possible to extract common methods, to avoid the implementation of the logic to replicate to a number of places to use.
2.3.5 Example
    • For example, define a method to get the width of the screen, according to the above principle, it can be defined as a static method: public static int getscreenwidth ();
    • For example: public static int getscreenwidth ();( √
    • For example: public static int Getscreenwidth (); (x)
    • For example: public static int Getscreenwidth (); (x)
    • For example: public static int getwidth (); (x)
    • For example: public static int Getscreen (); (x)
    • For example: public static int getsw (); (x)
2.4 Class naming specification 2.4.1 type

Class Naming conventions

2.4.2 Description

The class name mainly represents the function of a class, need to be concise, look at the text to understand, and capitalize the first letter.

2.4.3 Rules
    1. Capitalize the first letter;
    2. Java hump naming;
    3. The principle of knowing the meaning of the text;
    4. Ability to describe the function and main role of the class (the role of annotations);
    5. The Acitivity class ends with acitivity;
    6. The fragment class ends with fragment;
    7. Service class ends with service;
    8. The Broadcastreceiver class ends with receiver;
    9. The ContentProvider class ends with provider;
    10. The application class ends with application;
    11. The custom view class ends with Custom**view;
    12. The custom adapter class ends with adapter;
    13. The Viewholder in adapter ends with holder;
    14. Entity beans end with model;
2.4.4 Notes

No

2.4.5 Example
    • For example, defining a tool class to get screen information can be defined as public class screenutils;
    • For example: public class screenutils; √
    • For example: public class screenutils; (x)
    • For example: public class screen; (x)
    • For example: public class screenutils; (x)
    • For example: public class screen; (x)
    • For example: public class Su; (x)
2.5 Interface naming Specification 2.5.1 type

Interface Naming conventions

2.5.2 Description

Interface naming needs to be simple and clear, the length should not be too long;

2.5.3 rules
    1. Capitalize the first letter (the second letter is also uppercase);
    2. Java hump naming;
    3. The principle of knowing the meaning of the text;
    4. It is recommended to append "I" in front of the name;
2.5.4 Notes
    • I**listener
    • I**callback
    • i**;
2.5.5 Example
    • Example: Define an activity's method interface to implement some methods in the interface: public
      Interface Ifunctionlistener;
    • For example: public interface Ifunctionlistener; (√)
    • For example: public interface baseactivity; (x)
    • For example: public interface Baseactivityinter; (x)
    • For example: public interface Baseinter; (x)
    • For example: public interface Activityinter; (x)
2.6 Package Name Specification 2.6.1 type

Package name Specification

2.6.2 description

For classification management class files;

2.6.3 Rules
    1. All letters lowercase;
    2. Simple and clear, the level is very deep, there is no stitching the package name;
    3. Hope the text knows the meaning;
    4. Divide the package name by function, such as "my"
    5. Tool class can be divided into a tool class package name, Utils, inside can add the package name level;
    6. System class can be divided into a system class package, system, which can add the package name hierarchy;
    7. Component class can be divided into a package of components,*, add adapter package name, custom view package name;
    8. Service class can be divided into a services class package, service, inside can add the package name level;
    9. Database related classes can be divided into a database class, DB, which can add database related classes, Bean class, database service class, etc.
    10. Broadcast class can be divided into broadcast classes of packets, receiver, you can put some broadcast related classes;
    11. Network class related can be divided into, network, put some networks related classes;
    12. The fragment class is stored under the fragment package;
    13. Activity classes are stored under activity packages;
2.6.4 Notes

No

2.6.5 Example

No

2.7 Directory name Specification 2.7.1 type

Directory name specification

2.7.2 Description

The main is some jar package, so file configuration directory name;

2.7.3 Rules
    1. All lowercase letters;
    2. Simple and clear;
    3. Hope the text knows the meaning;
    4. Hump expression;
2.7.4 Notes

No

2.7.5 Example
    • Later, the possibility of adding a directory is not many, and now lists the directory structure that exists in the system:
    • LIB: The Save path of the third-party jar;
    • The directory of so files referenced by Jnilibs:jni;
2.8 Layout file name specification 2.8.1 type

Layout file name specification

2.8.2 description

Mainly contains the resource file naming problem;

2.8.3 Rules
    1. All lowercase letters;
    2. The middle is connected with "_";
    3. The principle of knowing the meaning of the text;
    4. The beginning of the layout file asks the class name;
    5. XML layout file name for list item: class name _item.xml;
    6. XML file name of activity class: class name _activity.xml;
    7. XML file name of the Fragment class: Class name _fragment.xml;
    8. The name of the XML file for the Custom view: Class Name _ Parent class name. xml;
2.8.4 Notes

No

2.8.5 Example

For example, if you define an XML file name for h5activity, you can define it as h5.xml, try not to use uppercase letters, and so on.

2.9 drawable file name specification 2.9.1 type

Drawable file name naming specification

2.9.2 description

Mainly contains the resource file naming problem;

2.9.3 rules
    1. All lowercase letters;
    2. The middle is connected with "_";
    3. The principle of knowing the meaning of the text;
    4. The beginning of the layout file asks the class name;
    5. 11_22_33_44,44:selector,shape (approximately five or six, not defined for the time being); 33:src, BG, color (extensible, nullable); 22: state name or null; 11: Business Name
2.9.4 Notes

No

2.9.5 Example

No

2.10 Resource ID Specification 2.10.1 type

Resource ID naming specification

2.10.2 description

The definition of various resource ID issues;

2.10.3 rules
    1. All lowercase letters;
    2. The middle is connected with "_";
    3. The principle of knowing the meaning of the text;
2.10.4 Notes

You can consider the abbreviation of the component's name as a prefix, (the ID name in the same XML file cannot be duplicated) such as: component shorthand (capital letter abbreviation) _ Business Name
Components of TextView: Tv_pay_money
Component of button: Btn_pay_money
Components of EditText: Et_user_name
Linerlayout components: Ll_container

2.10.5 Example

Such as: For example, a TextView component, you can click the button for payment, you can define the ID as: Tv_pay_money;

3 Annotation Specification 3.1 class comments
在类、接口定义之前当对其进行注释,包括类、接口的目的、作用、功能、继承于何种父类,实现的接口、实现的算法、使用方法、示例程序等。/** * author:作者 * time:时间 * desc:描述 */
3.2 Method Comments
方法注释的模板:/** * desc:描述 * @param 参数名 参数描述 * @param 参数名2 参数描述 * @return 返回值类型说明 * @throws Exception 异常说明 */
3.3 Class member variables and constant annotations
3.4 Internal Logic comments
内部逻辑注释模板://支付成功if (response.getRet() == 0) {   Toast.makeText(H5Activity.this, "支付成功", Toast.LENGTH_LONG).show();   goToNext(response);}//支付失败else if (response.getRet() == -1) {  Toast.makeText(H5Activity.this, "支付失败", Toast.LENGTH_LONG).show();  //刷新当前页面  reflush(currentUrl);}
4 Code Order 4.1 Code order
在一个典型的Activity中代码的顺序如下:/*** author:sh* desc:该class的作用* time:yyyy-MM-dd**/public class ClassName {    //(1) 成员变量集合    //(2) 回调方法集合    若该类为activity,则:onCreate、**、onDestory;    若该类为Fragment、则:onCreateView、**、onDestory;    //(3) 其他方法集合}
5 Code style 5.1 curly braces Wrap
左大括号不换行,右大括号换行;class MyClass {    int func() {        if (something) {            // ...        } else if (somethingElse) {            // ...        } else {            // ...        }    }}
5.2 Parenthesis Spaces
if (condition) {    body();}                         // 推荐
5.3 Indent
    • 4 spaces are one unit of indent layout and do not use tab tab.
    • 8 spaces are indented as a newline, including function calls and assignments.
    • Instrument i =
      Somelongexpression_r (that, Notfit, on, one, line); Recommended
5.4 Length of each line
    • Try to avoid a line that is longer than 100 characters.
    • Exception: If the comment line contains a command example that is more than 100 characters or a URL literal, it can be more than 100 characters long to cut and copy.
    • Exception: The import line can exceed the limit, because very few people will read it. This also simplifies the writing of programming tools.
5.5 Declare one variable at a time
    • Recommend a one-line declaration, as this is conducive to writing comments;
    • int level; Indentation level
    • int size; Size of table
5.6 If-else Statements
if-else语句应该具有如下格式:if (condition) {    statements;}if (condition) {    statements;} else {    statements;}if (condition) {    statements;} else if (condition) {    statements;} else{    statements;}注意:if语句总是用”{“和”}“括起来,避免使用如下容易引起错误的格式:if (condition)  // 避免    statement;
5.7 For statement
一个for语句应该具有如下格式:for (initialization; condition; update) {    statements;}当在for语句的初始化或更新子句中使用逗号时,避免因使用三个以上变量,而导致复杂度提高。若需要,可以在for循环之前(为初始化子句)或for循环末尾(为更新子句)使用单独的语句。
5.8 While statement
一个while语句应该具有如下格式:while (condition) {    statements;}
5.9 do-while Statements
do {    statements;} while (condition);
5.10 Switch Statement
一个switch语句应该具有如下格式:switch (condition) {    case ABC:        statements;        /* falls through */    case DEF:        statements;        break;    case XYZ:        statements;        break;    default:        statements;        break;}每当一个case顺着往下执行时(因为没有break语句),通常应在break语句的位置添加注释。
6 Exception Specification 6.1 exception name

When defining an exception, the suffix name of the exception ends with Exception, and **exception;

6.2 Exception Description

As far as possible in English description, simple and clear;

6.3 Exception format
一个try-catch语句应该具有如下格式:try {    statements;} catch (ExceptionClass e) {    statements;}try {    statements;} catch (ExceptionClass e) {    statements;} finally {    statements;}
7 Other Specifications 7.1 The function of the source file is less than 2K

Generally speaking, the number of lines of source files can not be greater than 2K lines, too many words to consider the Split function, Split function, etc.;

7.2 Using TODO comments
    • For temporary, short-term, great, but imperfect code, use TODO comments.
    • Todo comments should contain all uppercase Todo, followed by a colon:
    • Todo:remove This code after the UrlTable2 have been checked in.
    • Todo:change this to use a flag instead of a constant.
      If the TODO comment is a "future to do" format).
7.3 Using custom Log

When you need to print log in the system, try to use the custom log, the custom log in the development environment will print the logs, the formal environment will not print the log.

7.4 Using custom Tags

When the system prints the log, use tag as much as possible with tab, agree to the tag flag.

This article synchronizes to GitHub: Https://github.com/yipianfengye/androidProject, Welcome to star and follow

Android Product Development (i)--Practical development 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.