Android Development Concise coding rules detailed

Source: Internet
Author: User
Tags comments deprecated finally block

Please use the UTF-8 format to view the code to avoid the occurrence of garbled Chinese.
As for the comments should be in Chinese or English, please make your own decision, according to the company or project requirements, the recommended use of English.

1. Organize the Code
1.1. Warnings are not allowed in Java code, and warnings that cannot be eliminated are @SuppressWarnings.
1.2. Remove unwanted packages, methods, variables, etc. to reduce zombie code.
1.3. Use the Lint tool to view and eliminate warnings and errors.
1.4. Use CTRL+SHIFT+F to format the code and then make adjustments.
1.5. Use Ctrl+shift+o to format the Import package.
2. Naming rules
2.1. Basic Principles
2.1.1. variables, methods, class naming to be justified, strictly prohibit the use of name1, name2 and other names.
2.1.2. Name cannot be too long, use shorthand or abbreviation appropriately. (preferably not more than 25 letters)
2.1.3. Method names start with a lowercase letter, and the first letter of each word is capitalized.
2.1.4. Avoid using names that are similar or differ only in size.
2.1.5. Avoid using numbers, but use 2 instead of to, with 4 instead of for, such as Go2clean.
2.2. Class, interface
2.2.1. All words are capitalized in the first letter. Use words that accurately reflect the class, interface meaning, function, and so on. Nouns are generally used.
2.2.2. interface with the I prefix, or able, ible, er, and other suffixes. such as iseriable.
2.3. Fields, constants
2.3.1. The member variable begins with M, and the static variable begins with S, such as Musername, Sinstance.
2.3.2. Constants are all capitalized, preceded by an underscore, such as Max_number.
2.3.3. Hard coding is prohibited in the code, and some numbers or strings are defined as common quantities.
2.3.4. For obsolete functions, it is often added to maintain compatibility @Deprecated
3. Note
Refer to the comments for the example class below.

4. Class Internal order and logic
4.1. Each class should be based on a certain logical structure to arrange the base member variables, methods, internal classes, etc., so as to achieve good readability.
4.2. Overall, to follow the public first, after protected, the last private, the function of the arrangement should also have a logical sequence, from heavy to light.
4.3. The following order is available for reference:
Define tag, generally private (optional)
Define public constants
Defining protected constants, inner classes
Define private variables
Define public method
Defining protected methods
Define Private method
5. Expressions and statements
5.1. Basic principles: The use of compact style to write code
5.2. See the example Class Codingruler for details

The code is as follows Copy Code
/**
* File name (optional), such as Xxx.java
*
* Version information (optional), such as: @version 1.0.0
*
* Copyright Statement (open source code generally need to add), such as: Copyright (C) 2010-2014 xxx Corporation.
*/
Package Com.ntes.ruler;

/**
* The general description of the class is placed here.
*
*
* @author Author name
* @since 2014-XX-XX
*/

@SuppressWarnings ("unused")
public class Codingruler {

/** Public constant Comment * *
public static final String Action_main = "Android.intent.action.MAIN";

/** private constant annotations (constants of the same type can be divided and tightly defined) * *
private static final int msg_auth_none = 0;
private static final int msg_auth_success = 1;
private static final int msg_auth_failed = 2;

/**-protected Member variable Comment * *
protected Object mObject0;

/** Private member Variable MObject1 annotation (member variables of the same type can be divided and compactly defined)
Private Object MObject1;
/** Private member Variable mObject2 comment * *
Private Object MObject2;
/** Private member Variable MOBJECT3 comment * *
Private Object mObject3;

/**
* For more than one row of annotations, define the variable in this way
*/
Private Object MObject4;

/**
* Public Method Description ...
*
* @param param1
* Parameter 1 describes ...
* @param param2
* Parameter 2 describes ...
* @param paramxx
* Parameter xx description ... (Note: Please align parameters, descriptions)
*/
public void dosomething (int param1, float param2, String paramxx) {
The following comment tags can be seen through the Eclipse built-in task plugin
Todo uses TODO to tag code, stating that there are functional code to write at the logo
Fixme uses Fixme to mark Code, stating that the code for the logo needs to be fixed, or even that the code is
Wrong, can't work, need to fix
XXX is used to mark the code with XXX, which indicates that the code of the identifier implements the function, but implements
The method is open to discussion, hope to improve
}

/**
* Protection Method Description ...
*/
@Deprecated
protected void dosomething () {
... implementation
}

/**
* Private Method description ...
*
* @param param1
* Parameter 1 describes ...
* @param param2
* Parameter 2 describes ...
*/
private void dosomethinginternal (int param1, float param2) {
... implementation
}

/**
* Conditional expression principle.
*/
private void Conditionfun () {
Boolean condition1 = true;
Boolean condition2 = false;
Boolean condition3 = false;
Boolean condition4 = false;
Boolean condition5 = false;
Boolean condition6 = false;

Principle: 1. All if statements must be included with {}, even if there is only one sentence, no statements with no {} are prohibited
2. In an expression that contains multiple operators, use parentheses to avoid operator precedence problems
3. When judging a lot of conditions, please wrap other criteria
if (condition1) {
... implementation
}

if (condition1) {
... implementation
} else {
... implementation
}

if (condition1)/* Prohibit the use of statements without {} * *
Condition3 = true;

if ((Condition1 = = Condition2) | | (Condition3 = = Condition4)
|| (Condition5 = = Condition6)) {

}
}

/**
* Switch statement principle.
*/
private void Switchfun () {

Principle: 1. In a switch statement, a blank line between the break and the next case
2. For those that do not require a break statement, use the/* Falls through/* to label
3. Please write the default statement and keep the integrity
int code = msg_auth_success;
Switch (code) {
Case Msg_auth_success:
Break

Case msg_auth_failed:
Break

Case Msg_auth_none:
/* Falls through * *
Default
Break
}
}

/**
* Loop expression.
*/
private void Circulationfun () {

Principle: 1. Try to replace the original for statement with the For each statement
2. The loop must have a condition or statement that terminates the loop to avoid a dead loop
3. The loop should be as short as possible, extracting the contents of the long loop into the method
4. The nesting layer should not exceed 3 layers to make the loop readable

int array[] = {1, 2, 3, 4, 5};
for (int data:array) {
... implementation
}

int length = Array.Length;
for (int ix = 0; ix < length; ix++) {
... implementation
}

Boolean condition = true;
while (condition) {
... implementation
}

do {
... implementation
while (condition);
}

/**
* Exception capture principle.
*/
private void Exceptionfun () {

Principle: 1. The exception is caught in order to handle it, typically outputting exception information in an exception catch block.
2. The work of resource release can be put into the Finally block section to do. such as the closure of Cursor.
try {
... implementation
catch (Exception e) {
E.printstacktrace ();
finally {

}
}

/**
* Other principles (finishing ...) )。
*/
private void Otherfun () {
Todo
}
}

Do not find related articles plug-ins, rapid increase in traffic

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.