Android interview and Development Expert code specifications and details, android interview

Source: Internet
Author: User

Android interview and Development Expert code specifications and details, android interview

Note

Rule 1: You must use javadoc to generate documents for classes. Not only is it a standard, but it is also recognized by various java compilers.
[Rule 2] The description of the file should be available at the beginning of the file, which should contain the following information:
(1) Copyright information;
(2) file name;
(3) function description;
(4) author;
(5) completion date;
(6) version information;
(1.1) eclipse template:
/**
* 2012 amsoft.cn
* Name: $ {file_name}
* Description: $ {todo }.
* @ Author $ {user}
* @ Date: $ {date }$ {time}
* @ Version v1.0
*/
Example:
/**
* 2012 amsoft.cn
* Name: AbDateUtil. java
* Description: Date Processing.
*
* @ Author is like a dream
* @ Version v1.0
* @ Date: 11:52:13
*/
Rule 3: The variable name must have a clear meaning. Add a comment as much as possible. An empty line is added to each variable. The format is as follows:
/** Error code */
Public String errorCode;
/** Error message */
Public String message;
[Rule 4] The comments should increase the clarity of the Code. The content should be clear and clear, and the meaning should be accurate to avoid the ambiguity of the comments. The purpose of code annotation is to make the code easier to be understood by developers who participate in program design at the same time and other subsequent developers.
Rule 5: Keep comments concise. The best annotation should be a simple and clear annotation. You only need to provide sufficient information so that others can understand your code.
[Rule 6] The comments should be consistent with the code. Modify the code and modify the corresponding comments. comments that are no longer useful must be deleted.
[Rule 7] The comment should be similar to the code described in it. The comment on the code should be placed above it and not below it. If it is placed above it, it should be separated by a blank line with the code above.

Name

Rule 1 describes the complete English descriptors of variables, fields, and classes.
Rule 2 is case-insensitive to improve the readability of the name. Generally, lowercase letters are used, but the first letter of the class and interface names and the first letter of any intermediate word should be capitalized.
[Rule 3] Except for some common abbreviations, we should try to use them as few as possible. If you use them, note them.
[Rule 4] avoid using long names (preferably up to 15 letters ).
Rule 5: Avoid using similar or case-sensitive names.
Rule 6: Avoid using underscores as names. The names of the first and last letters in the following dashes are generally reserved by the system. In addition to the pre-processing definition, user names are generally not required. More importantly, underlines often cause trouble and are difficult to input, so avoid using them as much as possible.
[Rule 7] for variable naming, it is prohibited to take a single character (such as I, j, k ...), in addition to specific meanings, it can also indicate its variable type and data type. However, I, j, and k are allowed as local cyclic variables.
[Rule 8] The package name principle should be composed of one lowercase word. If the package name is composed of multiple words, all words should be in lowercase.
[Rule 9] attributes must be named in accordance with the basic naming rules. We recommend that you follow the Hungarian naming rules. In some cases, if you need to clearly distinguish between the attribute and local variables in the method, we recommend that the attribute name be prefixed with "m", for example, mUser.
Rule 10: Class names should use full English descriptors, which are used in combination with uppercase and lowercase: the first letter of all intermediate words is capitalized. The first word of the class method name often uses a verb with a strong action color. Example: openAccount () queryUser (). Such conventions often enable people to see the name of a class method to determine its function. Although this Convention requires developers to do more input work, because the class method name is often long, the return is to improve the comprehensibility of the Code.
[Rule 11] All static constant fields (static final) are separated by underscores (_) and uppercase letters. Example: PREFS_NAME.

Method

Rule 1: Limit the function size to less than 200 rows.
Rule 2: A method only provides one function. If a method implements multiple functions, consider splitting them into multiple methods. Each method implements a function.
[Rule 3] If multiple pieces of code repeat the same thing, you can consider providing a common method to implement this function for other methods to call.
Rule 4: Reduce recursive calls between functions.
Rule 5 checks the validity of all parameter input of the function. For example, if the input ArrayList object of the parameter is null, a program exception occurs if it is directly used.
[Rule 6] the exception of a method should be fully handled. Do not take it for granted that only one exception can occur in the functions implemented in the try block. In fact, many exceptions may occur, for example, IOException and NullPointerException. Add the default Exception to the thrown Exception and handle it. Example:
Try {
......
} Catch (SQLException sqle ){
......
// Catch all other exceptions
} Catch (Exception e ){
// Handle exceptions
......
}
Rule 7: handle the error return code of the called function carefully and comprehensively.
Rule 8 removes useless and unnecessary variables and code. Spam code in the program not only occupies extra space, but also may cause unnecessary trouble for program testing and maintenance.
Rule 9: a numeric constant in a for loop that acts as a counter value. Except for values 1, 0, and 1, code should not be written directly.
Rules

Rule 1: Release useless objects as soon as possible. Unnecessary objects should be promptly assigned to null.
String a = "one piece of memory ";
A = null;
Rule 2: Create an object or variable in the method. Some locally used objects or variables should be defined in the method. Once the method is returned, the objects created in the method meet the GC collection conditions, except the objects returned. In the following example, after the method is returned, a, B, and strBuf match, and c do not.
Private static String getSay (){
 
StringBuffer strBuf = new StringBuffer ();
String a = "hello ";
String B = "James ";
String c = null;
StrBuf. append (a). append (B );
C = strBuf. toString ();
 
Return c;
 
}
Rule 3: Set data types, including arrays, linked lists, and other data structures. The collection of these data structures is more complex for GC, And the unused reference objects should be assigned null as soon as possible. Objects defined in the following two sections of Code cannot be released, because these objects are still referenced in the collection. to release these objects, they must be removed from the collection, in the simplest way, set the set to null directly.
// Code segment 1
List <Item> items = new ArrayList <Item> ();
For (int j = 0; j <100; j ++ ){
Item item = new Item (String. valueOf (j ));
Items. add (item );
Item = null;
}
 
 
// Code segment 2
Item item = null;
For (int j = 0; j <100; j ++ ){
Item = new Item (String. valueOf (j ));
Items. add (item );
Item = null;
}
Rule 4: Use as few global variables as possible, static global variables, and local variables as much as possible. Both methods are static storage, but with different scopes. Common global variables act on the entire source program, while static global variables act on the defined source file, that is, other source files cannot be accessed. Changing a local variable to a static variable changes its storage method, that is, its lifecycle. Changing a global variable to a static variable changes its scope. Therefore, in principle, the local variable will never use the global variable.
[Rule 5] the memory of static variables is shared and only one copy is available during the running of the program. Therefore, sometimes some variables are declared as static, which can save memory space, however, because static variables have a long life cycle and are not easily recycled by the system, it is necessary to use static variables reasonably and not blindly.
[Rule 6] private constants in the class should be declared using final as much as possible. constants in the program for global use can be declared as static final.
Rule 7: Use the finalize function as little as possible. The finalize function is an opportunity that Java provides to programmers to release objects or resources. But it will increase the GC workload.
Rule 8: when the program has a certain waiting time, You can manually execute System. gc () is used to notify the GC to run, but the forced memory recovery has a negative impact on the system's automatic memory recovery mechanism, which will increase the processing time of the system's automatic recovery, therefore, try to avoid explicitly using System. gc ().
Rule 9: Use the finalize function as little as possible. The finalize function is an opportunity that Java provides to programmers to release objects or resources. However, it will increase the GC workload, so use finalize as little as possible to recycle resources.
Rule 10: when too many image resources are used, SoftReference is used for caching. Resources defined by soft references are automatically reclaimed when the memory is insufficient. It can guarantee to the maximum extent that no OutOfMemory exception is generated. It is generally used for cache implementation. However, the disadvantage is that the initialization of soft referenced objects is time-consuming. We also need to be responsible for rebuilding the recycled objects, it is troublesome.
[Rule 11] Use WeakReference as appropriate ). When you define an object and remember it with a weak reference, it will be recycled as a useless object in the next GC runtime. The difference between it and soft reference is that it will not be determined based on memory conditions.
Name
Rule 1: The name of the layout file is a combination of all lower case letters and lower horizontal lines. Each English descriptor is divided by a lower horizontal line (preferably no more than 3) and can only start and end with a letter.
Rule 2: The name is defined in the same way as Java.
[Rule 3] prompts and text information must be defined in string. xml.

Rules
[Rule 1] for database operations, remember to close the cursor and database. When SimpleCursorAdapter is required, you can use Activity. startManagingCursor (Cursor c) to manage the Cursor so that the lifecycle of the Cursor is consistent with that of the Activity;
[Rule 2] The cached convertView must be used when customizing the Adapter. For the standard method, see the relevant chapter of ListView;
[Rule 3] When a Bitmap object is not used, call the recycle () method to release the memory, and then assign the object null;
[Rule 4] distinguishes Application Context and Activity Context, so that objects with long life cycle cannot be referenced by Activity Context, so that the Activity cannot be destroyed. For objects with long life cycle, Application Context can be used.
[Rule 5] When you configure Activity in the AndroidManifest. xml file, you can add configurations to prevent the Activity from being reloaded During Horizontal/vertical screen switching. android: configChanges = "orientation | keyboardHidden | navigation ".
[Rule 6] for internationalization and maintainability, configure the String in String. xml and obtain it using this. getResources (). getString (int id) method in Java code.
[Rule 7] dip and sp are recommended for layout to adapt to different resolutions and pixel density.

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.