Android Java Class Authoring specification

Source: Internet
Author: User

This article is only my personal in the actual development of the habit of writing, of course, this way is also from a lot of official recommendations, so to a certain extent can be copied to apply. This article will not be updated regularly ~

use a special escape sequence instead of its octal

We should use special transfer sequences, such as: (\b, \ t, \ n, \f, \ r, \ ", \" and \) instead of their octal counterparts, such as: \012

Bold Use non-ASCII characters

If we need to use non-ASCII characters in practice, we can certainly go to the table to get their escape code, but it's not necessary. You can boldly write this in your code, or add a comment. Should be so easy to read.

String Unitabbrev = "μs";

import do not use wildcard characters

Do not show an import statement like this:import java.util.*;

Do not ignore curly braces

Curly braces if, else, for, do, while are used with statements, even if there is only one statement (or null), you should write the curly braces.

when do I change lines?

For non-empty blocks and block structures, braces follow Kernighan and Ritchie styles
(Egyptian brackets):

    1. No line break before opening curly braces
    2. Wrap after opening curly braces
    3. Wrap before closing curly braces
    4. If the closing brace is a statement, a function body, or the termination of a class, the closing curly brace is followed by a newline; Otherwise, the line is not wrapped. For example, if the right curly brace is followed by an else or a comma, the line is not wrapped.
Boolean true ;         if (isshow) {        else  {                    }

Truncate code line Wrapping

If a line of code is too long, you might need to wrap it. The principle of wrapping at this time is: Break at "non-assignment operator". Here are a few examples:

"+" on the next line

String url = "http://blog.isming.me/2014/10/27/"                 + "google-java-style/";

"&" on the next line

if true                 true ) {                    }

“." On the next line

Logger             . Init (your_tag)               //  default Prettylogger or use just init ()             . setmethodc Ount (3)            //  default 2             . Hidethreadinfo ()             //  default shown             . Setloglevel (Loglevel.none);  // default Loglevel.full

Variable declaration

Declare only one variable at a time, not: int A, B;

The following methods are recommended:

int A; int b;

The brackets in the array

Brackets should be part of the type, do not write in C + +, and do not have spaces between the brackets and the left. It should look like this:

int [] D

Default in Switch-case

Each switch statement contains a default statement group, even if it does not contain any code.

Break in the Switch-case

If your last judgment and your next judgment need to be executed in the same code, then you don't have to break, but you have to add a comment where the original break was.

Switch (input) {            Case 1:            case 2:                prepareoneortwo ();                   // Break ;                 // Fall through             Case 3:                handleonetwoorthree ();                  Break ;             default :                handlelargenumber (input);        }

The Order of qualifiers

Our variables or methods may have multiple qualifiers, such as public,static or something, then write in the following order.

 Public protected Private Abstract Static Final transient volatile synchronized native STRICTFP

eg

 Public Static Final String TAG;

The extraction of the package name

The package name is all lowercase, and consecutive words are simply connected and cannot be underlined .

Class name

The class name is usually a noun or noun phrase, and the interface name can sometimes be an adjective or an adjective phrase.

Method name

The method name is usually a verb or a verb phrase.

Abnormal

In general, the exception caught is the need to print out the log, but some exceptions are we know, or here the exception is inevitable, we can not capture, but need to add comments.

If a caught exception is named expected , it can be ignored without comment. The following is a very common scenario to ensure that the method being tested throws an expected exception.
Therefore there is no need to annotate it here.

Try {  emptystack.pop ();   Catch  expected) {}

Javadoc Mark

The standard Javadoc tags appear in the following order:,,, @param @return @throws @deprecated before these 4 kinds of tags appear, the description cannot be empty.
When the description cannot be accommodated in a row, successive lines need to be indented at least 4 more spaces.

/**      * Adds a {@link  Fragment} to this activity ' s layout.     *     @param  containerviewid the container view to where add the      *                        fragment. ( The line is indented)     *                             @param  fragment the fragment to be        added.      */

In addition to the first paragraph, each paragraph has a label before the first word <p> , and there are no spaces between it and the first word.

Android Java Class Authoring 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.