Android Development Coding Specification (for personal use)

Source: Internet
Author: User
Tags constant definition checkstyle

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?
  1. /**
  2. * @Title: ${file_name}
  3. * @Package ${package_name}
  4. * @Description: ${todo} (describe what the file does in a word)
  5. * @author Name
  6. * @date ${date} ${time}
  7. * @version V1.0
  8. */
  9. Types
  10. /**
  11. * @ClassName: ${type_name}
  12. * @Description: ${todo} (here is a word describing the role of this class)
  13. * @author Name
  14. * @date ${date} ${time}
  15. *
  16. * ${tags}
  17. */
  18. Fields
  19. /**
  20. * @author Name
  21. * @date ${date} ${time}
  22. * @Fields ${field}: ${todo} (describe what this variable means in a sentence)
  23. */
  24. Constructors
  25. /**
  26. * <p>title: </p>
  27. * <p>description: </p>
  28. * ${tags}
  29. * @author Name
  30. * @date ${date} ${time}
  31. */
  32. Methods
  33. /**
  34. * @author Name
  35. * @date ${date} ${time}
  36. * @Title: ${enclosing_method}
  37. * @Description: ${todo} (here is a word describing the effect of this method)
  38. * @param ${tags} settings file
  39. * @return ${return_type} return type
  40. * @throws
  41. */
  42. overriding methods
  43. /**
  44. * @author Name
  45. * @date ${date} ${time}
  46. * <p>title: ${enclosing_method}</p>
  47. * <p>description: </p>
  48. * ${tags}
  49. * ${see_to_overridden}
  50. */
  51. Delegate methods
  52. /**
  53. * @author Name
  54. * @date ${date} ${time}
  55. * ${tags}
  56. * ${see_to_target}
  57. */
  58. Getters
  59. /**
  60. * @author Name
  61. * @date ${date} ${time}
  62. * @return ${bare_field_name}
  63. */
  64. Setters
  65. /**
  66. * @author Name
  67. * @date ${date} ${time}
  68. * @param ${param} to set the ${bare_field_name}
  69. */
  70. 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)

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.