Android interview, Development of the master coding specifications and details
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Comments</strong></p></p><p><p>Rule 1 must use Javadoc to generate a document for the class. Not only is it a standard, it is also a method that is recognized by various Java Compilers.<br>"rule 2" should have a description of the file at the beginning of the file and should contain the following information:<br>(1) Copyright information;<br>(2) file name;<br>(3) function description;<br>(4) the author;<br>(5) Date of completion;<br>(6) version information;<br>(1.1) Eclipse Class Template:<br>/**<br>* ? amsoft.cn<br>* Name: ${file_name}<br>* Description: ${todo}.<br>* @author ${user}<br>* @date: ${date} ${time}<br>* @version v1.0<br>*/<br>Example:<br>/**<br>* ? amsoft.cn<br>* Name: Abdateutil.java<br>* description: Date processing class.<br>*<br>* @author's still a Dream.<br>* @version v1.0<br>* @date: 2013-01-18 11:52:13<br>*/<br>The "rule 3" variable name requires a clear meaning, as far as possible, add a comment, each variable in the middle of a blank line, the format is as Follows:<br>/** Error code */<br>Public String errorCode;<br>/** error Message */<br>Public String message;<br>"rule 4" comment Should increase the clarity of the code, the content should be clear, explicit, meaning accurate, to prevent the ambiguity of the Annotation. The purpose of the code comment is to make the code easier to understand by developers who are involved in the program as well as other subsequent Developers.<br>"rule 5" keeps the annotations Concise. The best comments should be simple, straightforward comments. Just provide enough information to enable others to understand your code.<br>The "rule 6" comment should be consistent with the Code. Modify the code at the same time to modify the corresponding comments, no longer useful comments to Delete.<br>The "rule 7" comment should be similar to the code it describes, and the comment on the code should be placed above it and not placed below, such as above, separated from the code above with a blank line.</p></p><p><p><strong>Named</strong></p></p><p><p> Rule 1 uses the full English descriptor that can accurately describe the VARIABLE/FIELD/CLASS. The <br> Rule 2 uses a mixed case to improve the readability of the Name. In general, lowercase letters should be used, but the first letter of the name of the class and interface, and the first letter of any intermediate word, should be capitalized. <br> "rule 3" In addition to some of the commonly recognized abbreviations, as far as possible abbreviations, if the use of abbreviations must be annotated. <br> Rule 4 avoids the use of long names (preferably no more than 15 letters). <br> Rule 5 avoids using names that are similar or only differ in Case. <br> Rule 6 Avoids the use of underscores as Names. The name of the underlined first letter is usually reserved for the system, except for the preprocessing definition, which is generally not used as a user Name. More importantly, underlining is often troublesome and difficult to enter, so try to avoid it. <br> Rule 7 is named for the variable, and it is forbidden to take a single character (such as i, j, k ...). ), In addition to the specific meaning, but also to indicate its variable type, data type, etc., but i, j, K as a local loop variable is Allowed. The name principle of the <br> Rule 8 package should consist of a lowercase word, and if the package name consists of multiple words, all the words should be lowercase. The naming of the <br> Rule 9 property must follow the naming basic specification and is recommended to be named according to the Hungarian Nomenclature. In some cases, it is recommended that the property name be prefixed with "m", for example: muser, If you need to explicitly differentiate the attributes and the inner local variables of the Method. The naming of the <br> Rule 10 class method should be in the full English descriptor, mixed in case: capitalize the first letter of all intermediate WORDS. The first word of a class method name often takes a verb with a strong action Color. Example: openaccount () Queryuser (). Such conventions often make it possible for a person to see the name of a class method to determine its Function. While this convention allows developers to do more input work, the class method names are often longer, but the payoff is to improve the understanding of the Code. The <br> Rule 11 static constant fields (static Final) all use uppercase letters, and the words are separated by underscores. Example: Prefs_name. <br> <br> <strong> methods </strong> </p></p><p><p>The size of the rule 1 function is limited to as much as 200 lines.<br>"rule 2" A method accomplishes only one piece of Functionality. If a method implements multiple functions, one can consider splitting into multiple methods, each implementing a Function.<br>"rule 3" If multiple pieces of code do the same thing over and over again, consider providing a common way to implement this function for other methods to Invoke.<br>"rule 4" reduces the recursive invocation between the function itself or the Function.<br>"rule 5" checks the validity of all parameter inputs of the Function. For example, the parameter passed in to the ArrayList object is empty, and if used directly causes a program Exception.<br>"rule 6" should deal with the exception of the method, do not take for granted that only one exception may occur in the function implemented in the try block, in fact, many exceptions can occur, such as: ioexception, nullpointerexception, etc. The exception that is thrown should be added to catch the default Exception and be Processed. Cases:<br>try{<br>......<br>}catch (SQLException Sqle) {<br>......<br>Capture all the other exception<br>}catch (Exception E) {<br>Handling Exceptions<br>......<br>}<br>"rule 7" The error return code of the called function is handled carefully and comprehensively.<br>"rule 8" removes unused, unnecessary variables, code. The garbage code in the program not only takes up extra space, but also is likely to cause unnecessary trouble to the test and maintenance of the Program.<br>Rule 9 is a numeric constant in the for loop that acts as a counter value and should not be written directly to the code except for -1,0 and 1.<br><strong>Rules</strong><br></p></p><p>"rule 1" releases useless objects as early as Possible. Unused objects should be assigned null in a timely manner.<br>String a = "piece of memory";<br>A = null;<br>Create an object or variable within the "rule 2" method. Some locally used objects or variables should be defined within the method, but the method returns, the object created within the method conforms to the GC recycle condition, except for the returned Object. In the following example, when the method returns, a, b, strbuf, and C do not conform.<br>private static String Getsay () {<br><br>StringBuffer strbuf = new StringBuffer ();<br>String A = "hello";<br>String B = "xiaoming";<br>String C = null;<br>Strbuf.append (a). Append (b);<br>c = strbuf.tostring ();<br><br>Return c;<br><br>}<br>"rule 3" collection data types, including arrays, linked lists and other data structures. These data structures are more complex for GC and should be NULL for unused reference objects as early as Possible. The objects defined in the following two paragraphs of code cannot be freed, because these objects are also referenced in the collection, and to release these objects must be removed from the collection, the simplest way to set the collection to null Directly.<br>Code Snippet 1<br>list<item> items = new Arraylist<item> ();<br>For (int J=0;j<100;j++) {<br>Item item = new Item (string.valueof (j));<br>Items.Add (item);<br>item = null;<br>}<br><br><br>Code Snippet 2<br>Item item = null;<br>For (int J=0;j<100;j++) {<br>item = new Item (string.valueof (j));<br>Items.Add (item);<br>item = null;<br>}<br>"rule 4" use as few global variables as possible, static global variables, and use local variables Instead. Both of these methods are static storage, but the scope is different, in which the ordinary global variables acting on the entire source program, and static global variables acting in the defined source file, that is, the other source files can not be Accessed. Changing a local variable to a static variable changes its storage mode, which is the life cycle, changing the global variable to a static change of its scope, so the principle can use the local to never use the Global.<br>"rule 5" static variable in the program while its memory is shared, and only one copy, so sometimes some variables declared as static, can play a role in saving memory space, but because the static variable life cycle is very long, not easy to be recycled by the system, so use static variables to be reasonable, can not blindly use.<br>The private constants in the "rule 6" class are declared as final as possible, and the constants used for global use in the program can be declared as static final.<br>"rule 7" uses the Finalize function sparingly. The Finalize function is a chance that Java provides programmers with an opportunity to release objects or Resources. But it will increase the workload of the GC.<br>"rule 8" When the program has a certain waiting time, you can manually execute SYSTEM.GC (), notify the GC to run, but forced memory recycling for the system automatic Memory recycling mechanism will have a negative impact, will increase the system automatic recovery processing time, so should try to avoid the explicit use of System.GC ().<br>"rule 9" uses the Finalize function sparingly. The Finalize function is a chance that Java provides programmers with an opportunity to release objects or Resources. however, It will increase the amount of GC work, and therefore minimize the use of a Finalize method to reclaim Resources.<br>"rule 10" uses a soft reference (softreference) cache when using too many picture Resources. Resources defined with soft references are automatically reclaimed by the system when there is not enough memory. To maximize the guarantee does not produce outofmemory exception, generally for the implementation of the cache, but the disadvantage is that the soft reference object initialization is time-consuming, we also have to take charge of the recovery after the object of the reconstruction work, more Trouble.<br>"rule 11" uses weak references (weakreference) as Appropriate. When you define an object and remember it with a weak reference, it will be recycled as a useless object the next time the GC runs, and the difference between soft references will not be judged by the memory Situation.<br><strong><strong>naming</strong></strong><br>The naming of the "rule 1" layout file uses a combination of all lowercase letters and a horizontal line, with each English descriptor with a lower horizontal line (preferably no more than 3), and can only start and end with a letter.<br>The definition of the "rule 2" name is the same as Java.<br>The "rule 3" hint and literal information must be defined in String.xml.<br><br><strong><strong>rules</strong></strong><br>"rule 1" Database operation, You should remember to close the cursor and database, when you need simplecursoradapter, you can use Activity.startmanagingcursor (cursor c) to manage cursors, Align the life cycle of the cursor with the life cycle of the activity;<br>"rule 2" must use cached Convertview when customizing adapter, and Standard methods refer to the relevant section of the LISTVIEW.<br>The "rule 3" Bitmap object should call the Recycle () method to free memory before it is used, and then assign the object null;<br>"rule 4" distinguishes between application context and activity context, which does not allow Long-life objects to refer to activity context, which prevents activity from being destroyed, and for objects with long life cycles, You can use Application Context.<br>"rule 5" When you configure activity in a Androidmanifest.xml file, you can add a configuration to prevent reloading activity,android:configchanges= when you switch between the screens and the screen orientation| Keyboardhidden|navigation ".<br>"rule 6" for internationalization and maintainability, Configure the string in string.xml, which is obtained in Java code through the this.getresources (). getString (int id) method.<br>"rule 7" in order to adapt to different resolutions, different pixel densities, layout is recommended using dip, text using SP.<br></p><p><p>Android interview, Development of the master coding specifications and details</p></p></span>
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