Technical Summary-android (III)-code specifications and coding specifications-android --

Source: Internet
Author: User

Technical Summary-android (III)-code specifications and coding specifications-android --
Naming rule variable name: 1) try to get a meaningful name. For example, the member variable of a user name should be written as username, rather than just a string. 2) if it is a constant, this value will not change during the encoding process. It should be written as an upper-case name, such as USER_AGE or User_Age, if it is composed of multiple words, you need to add an underline under each word to distinguish, the entire name is as follows: static final int USER_AGE = 25.
Method Name: Same as the variable name, the method name should also be meaningful, although you can understand, however, you may not be able to understand what it means. For example, if you want to write a method to update a name, you should write updateUserName successfully, instead of simply taking a name. The first letter of the method name must be in lower case, and the first letter of the subsequent word is in upper case.
Class Name: The first letter must be in upper case, for example, AddUserActivity, which is similar to the method name, except that the first letter of each word must be in upper case.
Android Control name: <Button android: id = "@ + id/update_username "... /> or <Button android: id = "@ + id/updateUserName "... /> I personally recommend that you use the first method to initialize the control in activity or fragment, and try to use the same name as the one written in the xml file, which is easy to manage.
Layout file name: Generally, it starts with activity. For example, activity_add_user.xmlor can start with fragment if fragment is used, for example, fragment_add_user.xml.
The xml file corresponding to the adapter, named row_user.xml
Annotations must be added to each class name to explain the role of the class or the introduction of the class. We recommend that you add annotations to each method name, for example: /***** Add User **/private void addUser (){...}
Encoding Rule 1) do not process complex operations and logic in the main thread, such as obtaining images from the server, updating the interface, etc. Use asynchronous methods to obtain data and use another sub-thread for work, update it to the interface to prevent ANR. 2) do not use if... else ..., this will make the program bloated and run less efficiently. You can replace if... else ...; 3) do not put the code of the initialization control in the oncreate method. This code is not beautiful and is not easy to modify. You can create another method to initialize the control, then the call in oncreate is OK; 4) ViewHolder should be used in the Adapter to save resources; otherwise, ANR may easily occur; 5) some constants can be written in the same class for management, reduce the compilation of repeated code; 6) if the String you create needs to change its value, we recommend using StringBuilder. If it does not need to be changed, it should be declared as a constant of static final; 7) use the switch statement. 8) You can use ImageLoader to asynchronously load images, or you can better introduce it. After downloading images, they can be saved in the cache, the next time you read the data, you don't have to get it from the server. You can directly read the cache, which can reduce the load on the server and increase the retrieval speed;
Of course, there are also some optimizations to list, optimize Android performance, and optimize the Android UI, which must be mastered by Android practitioners. If some people do not understand, I will introduce them one by one.
There are other things I want to update.

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.