Coding standard embodies a developer's basic qualities, good coding standards can improve the efficiency of the team coding, avoid a lot of unnecessary problems. Today to share a standard Java coding specifications for everyone, hope for the future development work to bring help.
the meaning of coding specification
In the development and maintenance of the project, coding specification, as a part of development specification, is very important and necessary, it is not only to improve the development efficiency, but also to reduce the cost of later maintenance and development. The fundamental purpose of the coding specification is to make it easy to understand not only the code, but also the purpose and meaning of the code routines written by the developers. This is used to reduce the difficulty and ambiguity of understanding the true functionality of the code in the project due to the replacement of the development maintainer or the lack of memory blur or confusion caused by prolonged maintenance. In addition, it also improves the efficiency and effectiveness of code review.
Specification Implementation Recommendations
Not to standardize and standardize, to improve the quality and efficiency of software development as the goal, supplemented by the IDE and other development tools for the protection, and gradually improve the level of coding standardization
For the requirements of some specifications, such as format specification, annotation specification, can be automatically formatted by using Eclipse/androidstudio's own Format method (shortcut key: ctrl+shift+f), which can improve the development efficiency and conform to the coding specification.
The code specification document itself needs to be constantly revised and perfected to conform to the requirements of the actual development specification.
Format specification
a) Indent
To format using a configuration file:
A tab in the configuration file equals 4 spaces.
b) The length of the President
100 characters per line
Note: When using the Format method (shortcut: Ctrl+shift+f) that comes with eclipse, you need to configure the "Maximum line width" Setting length of 100
c) Statement
d) Declaring variables, constants
A row declares only one variable or constant;
Declare the instance variable at the beginning of the code block, and do not declare "recommended" when the variable is first used
e) Declaring the class
The opening curly brace "{" is at the end of the declaration statement, and the closing brace "}" is another line;
Separated by a blank line between methods and methods
f) Statement
You can use the default "Control statements" formatting method of Eclipse when you have the Format method (shortcut: Ctrl+shift+f) that comes with Eclipse.
NOTE: The If statement is always enclosed in "{" and "}".
Example
class Example { voidBar () { Do{ } while(true);Try{ }Catch(Exception e) { }finally{ } }voidFoo2 () {if(true) {return; }if(true) {return; }Else if(false) {return; }Else{return; } }}
g) Use of spaces
There must be a space around the equals sign:
button = null;
The binocular operator must have a space around it:
ImageWidth = imagepadding + imageSize;
Punctuation must follow a space
Punctuation includes ",", ";" And so on, here are a few examples.
When a row defines multiple variables, "," followed by a space:
int I, J;
In the For Loop, ";" Followed by spaces:
for (int i = 0; i < count; ++i)
In a function call with multiple entry parameters, "," followed by a space:
Addcontentview (view, params);
h) Use of variable types
Programming as much as possible using interface programming, and less class programming.
Such as:
List<String>=new ArrayList<String>();
Naming conventions
General rules
The naming convention makes the program easier to read and easier to understand. They can also provide some information about the function of identifiers to help understand the code, for example, whether it is a constant, a package, or a class.
Package (Packages) A unique package name is always prefixed with all lowercase ascii letters and is a top-level domain name, usually com,edu,gov,mil,net,org, or the English double-character code of the identity country specified in the 1981 ISO 3166 standard. The subsequent parts of the package name vary according to the naming conventions of each organization. Such naming conventions may be distinguished by the composition of a particular directory name (department), project, machine, or registered name (login names).
Such as:
com.itotem.viewcom.itotem.utils.xxxx
Class (Classes) naming convention: The class name is a noun, in a mixed case, capitalized by the first letter of each word. Try to make the class name concise and descriptive. Use full words to avoid abbreviations (unless the abbreviation is used more widely, like url,html)
Such as:
publicclass Buttonpublicclass EditText
Interface (Interfaces) naming convention: The interface class name begins with the uppercase "I" and the casing rule is similar to the class name.
Such as:
publicinterface IProjGroupService
Method (Methods) method name is a verb, in the form of a mixed case, the first letter is lowercase, and then the first letter of the word is capitalized.
Such as:
publicvoidonCreate(Bundle savedInstanceState)publicvoidrun()
Local Variables are mixed in case, the first letter is lowercase, and then the first letter of the word is capitalized. Variable names should be short and descriptive. The choice of variable name should be easy to remember, that is, can indicate its use. 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,m and N, which are generally used for integer types. C,d,e, they are generally used for character types, and variable names should not start with an underscore or dollar sign.
Such as:
int0;float0;
Instance variable (Instance Variables) casing rules are similar to class names, except that one m is required.
Such as:
privateint0; private"";
If the instance variable is of type public, the same naming convention is used for local variables.
Such as:
publicint0; public"";
The declaration of a constant (Constants [with STIATC final modifier]) class constants should be all uppercase and the words separated by underscores. (similar to the C-language macro definition).
Such as:
privatestaticfinalint4; privatestaticfinalint999
Resource ID resource IDs are all lowercase, and the words are separated by underscores.
Note: This lowercase specification is enforced by Android and cannot be compiled if uppercase or special characters are present. Will error
Such as:
download app_name call_log_type
remark (Remark)
All identifier names require meaningful words and cannot use the names of styles such as myxxxx and Button01.
Additional Instructions
1, from the name can be intuitive to understand its definition and use, otherwise it must be added explanatory notes;
2. Naming must be uniform within the same system; avoid situations similar to those in the example;
Example: project group ID variable definition: pgid, Projectgroupid, Idprojectgroup, Idprojgroup
3, avoid the name too long, the name of the English abbreviation, avoid the use of Hanyu Pinyin "recommendation"
Organizational specifications
Introducing Package Specifications
The introduction of unused packages in classes is not allowed;
". *" cannot be introduced directly into the package and must be explicitly introduced into the class name
You can introduce packages by using shortcut keys. Ctrl+shift+o;
Annotation specification
A) General annotation rules
b) Description
Note to be concise and clear and easy to understand;
Keep comments in sync with your code.
Code quality is not good but can run normally, or have not implemented the code with//todo: task;
Code with//FIXME: declaration of the hidden error;
For classes or methods that are deprecated (deprecated), they must be added in their comments @deprecated
c) Javadoc annotation tag Syntax definition description
@author description of the class indicates the author of the development of such a module
@version the description of the class indicates the version of the class module
@see a reference to the description of classes, properties, methods, and related topics
Description of a parameter in a method @param description of the method
Description of the method return value @return The description of the method
@exception the description of the method to the exceptions that might be thrown by the method
@deprecated a description of a class or method this class or method is not recommended for use
d) Comments for class
Objective: To briefly summarize the role of this kind
Scope: All Java classes, can not include JavaBean
Writing specification: A class's comments must be written before the class's declaration syntax. In the comments, you want to describe the description of the class, the creator, and the date created.
Class Comment Template: Can be configured through Eclipse (code Templates new Java files)
${filecomment}${package_declaration}/** * Title: ${project_name}<br> * Description: <br> * Copyright: Copyright (c) ${year} <br> * Create DateTime: ${date} ${time} <br>* @author perry.li */${typecomment}${type_declaration}
Class Comment Example:
package cn.sh.sstic.projectmanagement.projectfeasibleschemaeval;
/** * Title: mwbas2008<br> * Description: 可行性方案套数数组定义类<br> * Create DateTime: Oct 6, 2008 4:41:03 PM <br>* @author perry.li */publicclass FormUtil {
e) Annotation of the method
Objective: To briefly summarize the function of the method, including its parameters, comments on the meaning of the return value
Scope: Various methods in the Java class
Note: The annotation of the implementation method of the interface should be written in the interface rather than in the implementation code;
There is no need to add annotations to the auto-generated Get/set method;
If the method allows null as a parameter, or if the return value is NULL, it must be stated in Javadoc, and if not stated, the caller of the method does not allow NULL as the argument and the return value is null safe.
Writing Specification: Method comments must be written before the method definition. The comment includes a simple description of the function of the method, the parameters of the method, the type of return value, and a simple description of the meaning of the return value.
Template: For a defined interface method, you can enter/** carriage return eclipse to automatically generate a comment template
Example:
/** * 演示方法注释 * @param args * @return * 返回 null 表示没有找到 * @throws Exception */ privatedemoFunctionthrows Exception{ returnnull; }
f) Comment for invalid code block
Purpose: Comment on a piece of code that is temporarily not enabled.
Note: This does not mean garbage, useless code, just temporarily not enabled or temporarily ambiguous code
Writing specification: The failure code block is annotated with the block annotation method line annotation method.
Note: The comment block needs to be configured when using the Format method (shortcut key: ctrl+shift+f) that comes with eclipse, removing the "Enable block commnet formatting" check
Example:
// if (1==1) { // // } else { // // }
Or
/* if (1 == 1) { // 如果1与1相等的时候 String code1; } else { // 如果1与1不相等的时候 String code2; }*/
g) Comments for Branch statements
Objective: To briefly describe the meaning of this branch condition
Writing specification: Comment on the next line of the branch statement code
Example:
if (1==1) { //如果1与1相等的时候 code else { //如果1与1不相等的时候 code }
h) Comments on variables, constants
Objective: To briefly describe the meaning of the variable and constant.
Writing specification: variables, constant comments must be written in the variable, constant definition, or the same line, simply describe the meaning of its representative.
Note: the variable (i,j,k) that is used for the self-loop may not require a comment.
Example:
String//提交标志i)@Override的使用所有的重写方法,在方法开始加上 @Override 关键字。
Such as:
@OverridepublicvoidonCreate(Bundle savedInstanceState) {}
Exception Handling Specification
The re-thrown exception must retain the original exception, which is the throw new Newexception ("message", e); Instead of writing the throw new Newexception ("message"), you cannot continue to throw an exception to the upper layer.
For important, captured business-related exceptions, create exception-handling classes that, after catching exceptions in the method, feed back to the user interface, prompting the user to "recommend"
Supplemental specifications
Before the code commits versioning, make sure that unnecessary log debug statements are cleared
Clear garbage or useless code must be removed
Android Development Advanced Technology QQ Group: 108721298 welcome into the group
Public Number: Mobilesafehome
(This public number supports voting)
Standard Java Coding Specification manual