Android Encoding Specification

Source: Internet
Author: User

The coding specification is especially important for programmers for several reasons:

In the life cycle of a software, 80% of the cost is maintenance;

Almost no software, in its entire life cycle, is maintained by the initial development;

Coding specifications can improve the readability of the software, allowing programmers to quickly and thoroughly understand the new code;

If you publish the source code as a product, you need to make sure it's packaged and clear, just like any other product you've built;

Name 1. Package naming

Package name rule: A unique package name is always prefixed with all lowercase ascii letters and is a top-level domain name, such as COM, edu, gov, net, org, and so on. The subsequent parts of the package name vary according to the naming conventions of each organization. Such naming conventions require the Division, project, module, etc. to be distinguished by the composition of a particular directory name.

For example: com.issmobile.sina.reader.activity-----------Sina Bookstore Project activity package, in the case of customers do not request, as far as possible to name the package name in this format.

2. Naming of classes and interfaces

Naming rules: Capitalize the first letter of each word, as concise and descriptive as possible. Use full words to avoid abbreviations (unless abbreviations are widely used).

For example:

Activity------[Xxx]activity.java eg:splashactivity

Dialog------[Xxx]dialog.java Eg:logindialog

Service-------[Xxx]service.java

Common Tool Class--[xxx]util.java

Abstract class------Abs[xxxx].java

interface------I[xxx].java

3. Name of the method

Naming rules: The first letter is lowercase, and then the first letter of the word is capitalized.

Names of common methods in a class:

The method of obtaining a class (typically with a return value) generally requires a get, such as Getfirstname (), Getlastname (), to precede the field name being accessed. In general, the Get prefix method returns a single value, and the method of the Find prefix returns the list value.

The method of setting the class (the general return type is void): Prefix the Access field name with a set, such as Setfirstname (), Setlastname ().

The method of judging the Boolean type of a class generally requires that the method name be prefixed with the word is or has, such as ispersistent (), isstring (). Or use a word that has a logical meaning, such as equal or equals.

General methods of the class generally use the full English description of the member method function, the first word as far as possible with the verb, the first letter lowercase, such as OpenFile (), Addcount ().

The construction method should be written in an incremental manner. (The number of arguments is written in the back).

ToString () Method: In general, each class should define ToString ().

4. Variable naming

Naming rules: The first letter is lowercase, and then the first letter of the word is capitalized. Try to avoid the variable name of a single character unless it is a one-time temporary variable. Temporary variables are usually named I, J, K, generally used for integral type, C, D, E, generally used for character type.

5. Name of member variable

Naming rules: Name the same variable, but do not add the word m before the private variable.

6. Constant naming

Naming rules: the Declaration of class constants, should be all uppercase, the words are separated by underscores.

For example: static final int min_width = 4;

static final int max_width = 999;

7. Exception naming

Naming rules: The naming of custom exceptions must end with exception to be explicitly represented as an exception.

8. Layout naming

Naming rules: Layout XML file naming must be all words lowercase, and the words are separated by an underscore.

Example: The activity layout file-------Activity_[xxx].xml

File referenced by layout file-----layout_[xxx].xml

Dialog layout file---------dialog_[xxx].xml

Adapter Item---------Item_[xxx].xml

9. ID Name

Naming rules: The name of the control ID is spelled by the name and function of the control, identifying the first letter of the name and function of the control in lowercase, followed by the first letter of the word, with an underscore connection ([control name]_[effect]).

Example: Edittext_username

Textview_message

Button_login

10. Resource naming

Naming rules: All resources in res (such as drawable,string, etc.) must be named with all the words lowercase, and the words separated by an underscore

Comments

Java programs have two types of annotations: implementation annotations (Implementation comments) and document comments.

Implement annotations using/*...*/and//.

Document comments using/**...*/, document annotations can be converted from Javadoc tools to generate HTML files

1. File comments

All source files should have a comment at the beginning that lists the class name, version information, date, and copyright notice.

/*

* File name

* contains list of class names

* Version information, version number

* Date created.

* Copyright Notice

*/

2. Class Comment

Each class should contain a comment in the following format to illustrate the function of the current class, etc.

/**

* Class Name

* @author Author <br/>

* The main functions of the implementation.

* Date Created

* Modify the date, modify the content.

*/

3. Method notes

Each method should contain comments in the following format, including the purpose of the current method, the meaning of the current method parameter, the contents of the current method's return value, and a list of thrown exceptions.

/**

*

* A sentence overview of the method

* <p> Method Details (simple method can not be detailed) </p>

* @params describes the meaning of the parameter

* @return indicates the meaning of the return value

* @throws IOException describes the condition in which this exception occurs

* @throws NullPointerException describes the condition in which this exception occurs

*/

4. class member variables and constant annotations

Member variables and constants describe the meaning of the current variable or constant by using a comment in the form of Java Doc.

/**

* xxxx meaning

*/

5. Other Notes

Comments inside the method if more than one line is required to use the/*...*/form if a single line is used//... Formal annotations. Do not use Java Doc annotations inside the method.

Common specifications 1. Blank Line

Try to use empty lines to separate logical-related code snippets to improve readability

You should always use a blank line in the following situations:

A Between two fragments (sections) of a source file

b Between class declarations and interface declarations

C Between two methods

D Between the local variables within the method and the first statement of the method

E Two logical segments within a method to improve readability

2. Method

One way to avoid more than 30 rows, if the method is too long, the current method of business logic is already very complex, then you need to split the method, please try to ensure that each method only one thing.

3. Parameters and return values

The parameters of a method should be as few as 4!

If a method returns an error code, use an exception!!

Try not to use NULL, instead of an exception, or use an empty variable, such as returning a List, you can use Collections.emptylist ()

4. Mystery of the number

Individual numbers, characters are not allowed in the code! If you need to use numbers or characters, encapsulate them as static constants by meaning! (except in the For statement)

5. Control statements

If there is a constant in the judgment, the constant should be placed on the right side of the judgment. Such as:

if (true = = ISAdmin ()) ...

Try not to use nesting of three-mesh conditions.

All if statements must be included with {}, even if there is only one sentence:

if (true) {

Do something ...

}

if (true)

i = 0; Do not use this

For loops:

Try to avoid the following ways

while (Index < Products.getcount ()) {

Every time the GetCount () method is executed,

If this method is time consuming, it will affect execution efficiency.

and may bring synchronization problems, if you have synchronization requirements, use synchronous block or synchronous method

}

Recommended Way

Saves the operation structure in a temporary variable, reducing the number of method calls

Final int count = Products.getcount ();

while (Index < count) {

}

6. Exception capture Processing

The usual idea is to use exception handling only for errors: logic and programming errors, setup errors, corrupted data, resource exhaustion, and so on.

The usual rule is that the system should not produce any exceptions in normal state and without overloading and hardware failure.

Minimizes the number of exceptions that are exported from a given abstract class. Do not use exceptions for predictable events that occur frequently. Do not use exceptions to implement control structures.

If there is a finally clause, do not return directly in the try block, nor in finally.

7. Access control

If there is not enough reason, do not declare the instance or class variable as public. Typically, instance variables do not need to be set (set) and get (gotten) explicitly, which is usually the result of the edge effect (side effect) of the method invocation.

A proper example of a variable with a public instance is a class that is only a data structure and has no behavior. That is, if you want to use a struct (struct) instead of a class (if Java supports structs), it is appropriate to declare the instance variable of the class as public.

8. Assigning values to variables

Avoid assigning the same value to multiple variables in a single statement. It's hard to read. For example:

Foobar.fchar = Barfoo.lchar = ' C ';

Do not use assignment operators in places that are easily confused with equality-relational operators. For example:

if (c + + = d++) {//avoid! (Java disallows)

...

}

should be written

if ((c + + = d++)! = 0) {

...

}

Do not use the inline (embedded) assignment operator to try to improve the efficiency of the runtime, which is the work of the compiler. For example:

D = (A = B + c) + R; avoid!

should be written

A = B + C;

D = A + R;

9. Parentheses

In general, it is a good practice to use parentheses in expressions that contain multiple operators to avoid operator precedence.

Even though the precedence of an operator might be clear to you, it may not be the case for others. You can't assume that other programmers are as aware of the precedence of operators as you do.

if (A = = b && c = = d)//avoid!

if ((a = = b) && (c = = d))//Right

10. return value

Try to make your program structure conform to the purpose. For example:

if (booleanexpression) {

return true;

} else {

return false;

}

This should be replaced by the following:

Return booleanexpression

In a similar way:

if (condition) {

return x;

}

return y;

should be written:

return (condition x:y);

11. Conditional operator "?" The previous expression

If an expression containing a two-tuple operator appears in the ternary operator "? : "The"? " Before, you should add a pair of parentheses to the expression. For example:

(x >= 0)? X:-X

Android Encoding Specification

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.