What are the specifications for Android development?

Source: Internet
Author: User

  • Encoding method
  • Unified with UTF-8. Android Studio is already UTF-8 by default, as long as you don't have to change it.

      1. Indent in
        With 4 spaces, the tab size is set to 4 to ensure that the TAB key is indented by 4 spaces. In addition, do not tick the Use tab character, you can guarantee to switch to a different tab length of the environment can also continue to maintain a uniform 4 space indentation style.

      2. Braces

    public void Method ()
    {
    Bad
    }
    public void Method () {
    Good
    }

      1. Use of spaces

    The IF, else, for, switch, while, and other logical keywords are separated by a space from the following statement.

    Good
    if (booleanvariable) {
    TODO while Booleanvariable is true
    } else {
    TODO Else
    }
    Bad
    if (booleanvariable) {
    TODO while Booleanvariable is true
    }else {
    TODO Else

    Each side of the operator is separated by a space.

    int result = a + B; Good, = and + are separated by a space on each side
    int result=a+b; Bad,= and + are not separated by spaces

    Each parameter of the method is separated by a space.

    public void Method (string param1, string param2); A space between the comma and string after good,param1
    Method (param1, param2); Good, when a method is called, a space is separated from the comma after param1 with param2
    Method (PARAM1,PARAM2); Bad, not separated by a space

      1. Use of Blank lines

    Separate logically related snippets with blank lines to improve readability. A blank line is also a blank row, not more than one line. A blank line is required in the following situations:

      • Between two methods
      • Between two logical segments within a method
      • The first logical statement of local variables and methods within a method
      • Between constants and variables
      1. Long line

    When an expression cannot fit within a row, a newline is displayed, and a new row is indented with 8 spaces.

    SomeMethod (LongExpression1, LongExpression2, LongExpression3,
    LongExpression4, LONGEXPRESSION5);

      1. One row declares a variable
        A row declares a variable and does not declare multiple variables on a line, which facilitates writing comments.

    Private String param1; Parameter 1
    Private String param2; Parameter 2

      1. The line width is set to 100, and the formatting automatically breaks to the line width position.

      2. Use shortcut keys to automatically format your code.

    Windows:ctrl+alt+l
    Mac:option+command+l

      1. A method should not exceed 40 lines of code.

      2. It is prudent to refer to third-party libraries

    Avoid the application of large-capacity third-party libraries, resulting in a very large client package.

      1. Unit of text size

    Unified with the SP, the element size unit is unified with DP.

      1. Strings in the app

    Unification is defined in Strings.xml and then referenced in code and layout files.

      1. Color values

    Unification is defined in Colors.xml and then referenced in code and layout files. Also, do not refer to the system's colors in code and layout files, except for transparency.

      1. JSON parsing

    Android in the call server-side interface generally return is JSON data, when parsing JSON, whether using the original manual parsing method, or using JavaBean parsing method, the parsed results must be used to empty processing. Does not allow problems with JSON on the server, causing the app to crash when parsing json.

      1. int type constant

    When the function returns, if the data returned by the int type is not a real practical data value (for example, width, height, size equivalence), only represents the function execution success, failure, the state value of the exception, and these values are a limited number of values, you must use static constant description of these values, or use the enumeration type, For example:

    int getjsonstring ()

    The function returns 1 for the parse JSON data exception, 0 for success, 1 for network connection exception, and 2 for the data portion of the JSON content to be empty. So do not use these literals directly in the code inside the function, these literals are meaningless to the programmer and the code is poorly readable, suggesting the following pattern:

    public static final int result_parse_json_exception =-1;
    public static final int result_success = 0;
    public static final int result_network_exception = 1;
    public static final int result_no_data = 2;

    The advantage of using these symbolic constant values instead of literals is that the symbolic constant value is made up of uppercase English words, which makes sense to help programmers better understand the meaning of the function return value, and the specific assignment of the symbolic constant value is also very convenient to modify at a later stage.

      1. Package Log function

    The log function should be encapsulated as the tag parameter that automatically turns the class name of the current class into the log output, preferably the published app is able to cycle through the log file to the system store, and the log file should be reused in a repeated overlay manner. The following is just an imperfect example:

    public class MyLog {
    public static final String TAG = "MyApp";
    public static void V (Object o, String message) {
    LOG.V (TAG + o.getclass (). Getsimplename (), message);
    }
    }

    How to use:

    LOG.V (TAG + o.getclass (). Getsimplename (), message);

    Print results

    V/myapp Mainactivity:hello

      1. Activity Click events
        The activity handles all the logic in a view.onclicklistener.

      2. The data must be tested.
        For example, the character type to the digital type, if the conversion failure must have a default value;
        Whether the service-side response data is effectively judged;

    What are the specifications for Android development?

    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.