Technical Summary--android (III.)--code specification and Coding specification

Source: Internet
Author: User

naming rulesVariable Name:1) Try to take meaningful names, such as: a user name of the member variables, should be written username, and not just write a string;2) If it is a constant, both in the encoding process, this value is not changed, should be written in uppercase names, such as: User_age, or can be user_age, here is to emphasize that, if it is composed of multiple words, you need to add an underscore under each word to distinguish, The whole name is this: static final int user_age = 25.
Method Name:Similarly, as with the name of the variable, the method name should also take a meaningful name, although you can understand, but for a person to see may not understand what is meant, for example: to write a method to update the name, should write a successful updateusername, instead of just take a name. method Name The first letter must be lowercase, followed by the first letter of the word is uppercase.
class Name:The first letter must be uppercase, such as: adduseractivity, in fact, similar to the method name, but the first letter of each word must be uppercase.
Name of Android control:<buttonandroid:id= "@+id/update_username".../>or<buttonandroid:id= "@+id/updateusername".../>The first way of personal adviceThe control is then initialized in the activity or fragment as much as it is written in the XML file, so it is easy to manage.
layout file name:In General, they start with activity, such as: Activity_add_user.xmlorIf you use fragment, you can start with fragment, such as: Fragment_add_user.xml
adapter The corresponding XML file, named: Row_user.xml
Noteseach class name should be annotated to explain the role of the class or the introduction of the class;It is recommended that you add comments to each method name, such as:/**** Add User**/private void AddUser () {...}
Encoding Rules1) in the main thread do not deal with complex operations and logic, such as fetching pictures from the server, updating the interface, and so on, should use asynchronous to obtain data, another sub-thread to work, and then update to the interface to prevent the occurrence of ANR;2) Do not blindly use if...else ..., this will make the program bloated and inefficient operation, you can replace the if...else in a different way ... ;3) Initialize the code of the control, do not put it all in the OnCreate method, such code is not beautiful and easy to modify, you can create another method to initialize the control, and then call in OnCreate OK;4) in adapter to use Viewholder, save resources, otherwise it is easy to appear ANR;5) Some constants can be written in the same class to manage, reduce duplication of code writing;6) If you create a string to change its value, it is recommended to use StringBuilder, if it is not changed, it should be declared as static final constant;7) Skillfully use the switch statement;8) Image asynchronous loading can use Imageloader, or better can be introduced, the picture can be saved in the cache after downloading, the next time you read the time will not be taken from the server, read the cache directly, so that can reduce the pressure on the server and get faster;
Of course there are some optimizations list, optimized Android performance, optimized Android UI, these are what Android practitioners have to master, if there are people who do not understand that I would like to introduce.
There are other updates that I think about.

Technical Summary--android (III.)--code specification and Coding 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.