Reprint Please indicate this article from CYM's blog (http://blog.csdn.net/cym492224103
), thank you for your support!
AndroidDevelop coding Specifications
Purpose and Guiding principles
Objective
Unified Specification Java Coding Styles and standards in Eclipse editing Environment
Guiding Principles
1) The first is to write the program, followed by the computer. This is the basic point of software development, software life cycle throughout the product development, testing, production, user use, version upgrades and later maintenance and other long-term processes, only easy to read, easy to maintain software code to have vitality.
2) Keep the code simple and clear and avoid excessive programming skills. Keeping the code simple is the basic requirement of software engineering. Don't overdo it, or it will reduce the readability of your program.
3) First of all, ensure the correctness of programming, and then consider efficiency. The first consideration of programming is to satisfy the quality factors of correctness, robustness, maintainability, portability, and finally to consider the efficiency and resource consumption of the program.
4) functions (methods) are written for a specific function, not a universal toolbox. The method is a processing unit that has a specific function, so it should be a good planning method, not all things are implemented in a single method.
5) Add more comments. Comments are meant to make it easier for people to understand the code, not to be perfunctory. For example, the sort function, where comments are written in "Sort ", is meaningless because the name is known as the sort. For example, the comment "hill sort implementation " makes sense, because the reader can know more information than the name.
Scope of application
Applies to all projects developed in the Java language.
Coding specifications
File
1) The project file must use a meaningful name. For example, the view file that is refreshed in Eclipse dropdown is named ' Refreshloadingview '.
2) for the file directory to be created as follows:
-Package name below: Program entry
-activity: Storage of all activity, different modules can be set up sub-package storage for easy to distinguish
-frament: Storage of all frament, different modules can be set up sub-package storage for easy to distinguish
-service: Storage of all service, different modules can be set up sub-package storage easy to distinguish
-broadcast: Storage of all broadcast, different modules can be set up sub-package storage for easy to distinguish
-view: Store all custom controls
-base: Store all base classes and store global variables
-utils: Store all tool classes
-adapter: Store all adapter
-DB: Storage database related such as: Model package (DB entity), operate package (DB operation Class)
-http: Store the request class and all request entities (DTOs), callback
For pictures, resource files, etc. are stored in the Android default storage path
Bluetooth , network request class , database operation base class encapsulated into Lib, reference can be.
Comments
Open eclipse-"Properties->code templates->comments
Configured as follows
Files
[Java]View Plaincopyprint?
- /**
- * @Title: ${file_name}
- * @Package ${package_name}
- * @Description: ${todo} (describe what the file does in a word)
- * @author Name
- * @date ${date} ${time}
- * @version V1.0
- */
- Types
- /**
- * @ClassName: ${type_name}
- * @Description: ${todo} (here is a word describing the role of this class)
- * @author Name
- * @date ${date} ${time}
- *
- * ${tags}
- */
- Fields
- /**
- * @author Name
- * @date ${date} ${time}
- * @Fields ${field}: ${todo} (describe what this variable means in a sentence)
- */
- Constructors
- /**
- * <p>title: </p>
- * <p>description: </p>
- * ${tags}
- * @author Name
- * @date ${date} ${time}
- */
- Methods
- /**
- * @author Name
- * @date ${date} ${time}
- * @Title: ${enclosing_method}
- * @Description: ${todo} (here is a word describing the effect of this method)
- * @param ${tags} settings file
- * @return ${return_type} return type
- * @throws
- */
- overriding methods
- /**
- * @author Name
- * @date ${date} ${time}
- * <p>title: ${enclosing_method}</p>
- * <p>description: </p>
- * ${tags}
- * ${see_to_overridden}
- */
- Delegate methods
- /**
- * @author Name
- * @date ${date} ${time}
- * ${tags}
- * ${see_to_target}
- */
- Getters
- /**
- * @author Name
- * @date ${date} ${time}
- * @return ${bare_field_name}
- */
- Setters
- /**
- * @author Name
- * @date ${date} ${time}
- * @param ${param} to set the ${bare_field_name}
- */
- Some important places are marked with//TODO
Coded typesetting format
You can use Eclipse to bring your own format shortcut Shift+command (ctrl) +f
Naming conventions
Method
1) The name of the method should all be made up of meaningful words, and begin with a lowercase letter, with a combination of multiple words, the first letter of the following words capitalized.
For example:
Getuserinformation ...
2) The method that sets the contents of the class variable should use set as the prefix, and the method that reads the contents of the variable should use get as the prefix.
For example:
GetUserName ();
Setusername (String username);
3) parameters in the method: parameter names make sense
For example:
Setusername (String username);
Variable
1) Variables must have a meaningful name, so that other members can easily read the meaning of variables, variable naming can be synonymous with the English name, you can use a few English words, the first letter lowercase, the other words first uppercase.
For example:
Private protected:
Mcountchild
Public
Local variables
Countchild
2) for some special types of variables, name to take the type, such as nsarray variable named Xxxarray, other such as xxxdictionary, xxxsize andso on. This allows you to know what type of variable is in the name.
Constant
1) Avoid the direct occurrence of constants in the program, the use of more than once should be in the form of a constant definition instead.
2) The macro definition of a constant should be consistent with the type it is actually used in. If you define a floating-point type in 3.0来, 3 represents an integer.
3) The name of the constant should be able to express its purpose and be expressed in uppercase letters.
For example:
public static final Doublepi 3.1415926
4) Some constants are preceded by special prefixes, which can be distinguished as different constants,
For example:
Userdefaultskey the variable before adding Udkey_,
Notificationnamekey Front plus nnkey_,
Dictionarykey Front plus dictkey_,
Class
1) All class names, interface names begin with uppercase letters, and when multiple words are combined, the first letter of the following words is capitalized. class, the interface name must be meaningful.
2) classes that inherit from UIView End with a view .
For example:
Operatorusersinfomationview, LabelView and so on.
3) classes that inherit from Viewcontroller end with Viewcontroller .
For example:
Homepageviewcontroller, Loginviewcontroller and so on. Other analogy.
4) All entities that hold data end with model.
For example:
Usermodel,
Abstract class:
start with abstract
Example: Abstractdatamanager
Interface:
start with I
Example: Idatamanager
Specification for control-specific prefixes
Control name prefix
Button/imagebutton BTN
ToggleButton TB
TextView TV
CheckBox CB
EditText ET
GridView GV
ListView LV
Videoview VV
ScrollView SCV
ImageView img
The control ID naming rules for XML files:
Control-specific prefix + English name (capital of each word first letter )
The English name is as brief as possible to reflect the general purpose of the control.
For example:
The button ID for adding a text message template can be named:
Btnaddsmscontent
local variables in the corresponding Java code can be named:
Btnaddsmscontent
the Class field in the corresponding Java code can be named:
Private protected:
Mbtnaddsmscontent
Public
_btnaddsmscontent
The ListView ID that displays the SMS template can be named:
Lvsmscontent
local variables in the corresponding Java code can be named:
Lvsmscontent
the Class field in the corresponding Java code can be named:
Private protected:
Mlvsmscontent
Public
_lvsmscontent
Modifying specifications
New Line of code
There should be a comment line description before and after the new line of code.
Modify the person, modify the time, modify the description
New Line of code
Modify End
Delete a line of code
Delete the code before and after the comment line description
Modify the person, modify the time, modify the description
Line of code to delete (comment on the statement to be deleted)
Modify End
Modify line of code
Modify the lines of code to comment on the old lines of code and then add new lines of code.
Modify the person, modify the time, modify the description
Modify the previous line of code to start
Modify the previous line of code
End of code line before modification
Line of code starts after modification
Modified line of code
Modify End
Error handling specification
1) The use of all entity classes and their properties to do non-null check, to avoid the use of empty objects caused by errors;
2) Cross-border check of the array;
3) All functions that have a return value must first specify the default value at the beginning of the function;
4) Common functions, methods and specific business implementation code must add try...catch error capture processing;
Code Management Specification
1) Unified use of SVN for code management
2) strictly control the number of commits, only in the key code or module complete function fully realized, before work to do code commit;
3) The code commit must indicate the change record;
4) The code must be Checkstyle before submission, and resolve the Checkstyle warning before submission;
CheckStyle
1. Checkstyle is a static code analysis tool in software development that checks whether Java source code conforms to coding rules.
2. Configure checkstylehttp://www.cnblogs.com/lanxuezaipiao/p/3202169.html
3. Configure The CheckStyle file configuration file to the (Chinese cue version) Checkstyle.xml
Configuration Method http://chenzhou123520.iteye.com/blog/1627618
4. using Checkstyle
Android Development Coding Specification (for personal use)