Android code specification and studio configuration Codestyle

Source: Internet
Author: User
Tags checkstyle

Studio configuration Codestyle can help us to detect the code standard, keep everyone's code unified, to see how to configure and use it

Code specification, a set of your own company

Code specification

First, Introduction

A. Purpose

This article provides a complete set of standards, conventions, and guidelines for writing efficient and reliable Java code. They are based on safe and reliable software engineering principles that make code easy to understand, maintain, and enhance. And, by following these program design criteria, you will be significantly more productive as a Java software developer. Experience has shown that if you take the time to write high-quality code from the start, it is much easier to modify the code in the software development phase. Finally, following a common set of program design standards will result in greater consistency, resulting in a significant improvement in the efficiency of the software development team.

Note: The purpose of the spec code is to make it easier for developers to understand other people's code than to constrain the way you encode it

B. Scope

This guide is only available for Android program code written in Java.

C. References

Http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

Http://source.android.com/source/code-style.html

Ii. Quick Start

L Hump-Type naming (case alternating)

Error:

Public Classmyclass

Publicinterface Foo

public finalstatic int max = 100

Public Intfoovalue

That's right:

Public Classmyclass

Publicinterface IFoo

public finalstatic int MAX = 100

Public Intfoovalue

L use Javadoc to recognize annotations

Add a comment before the function declaration (enter "/**" + carriage return)

L Thin function body (preferably not more than 40 lines)

L Local variables are used with declarations (minimizing the scope of variables)

L maximum 100 characters per line

L indent with 4 spaces instead of tab

L member variable name starts with m, static variable name starts with s

L Front Curly brace "{" Do not separate a line

Error:

if (condition)

{

DoSomething

}

That's right:

if (condition) {

Do something

}

L Multi-use standard annotations

Error:

publicvoidonCreate (Bundle savedinstancestate) {

That's right:

@Override

publicvoidonCreate (Bundle savedinstancestate) {

L Less use of uncommon abbreviations

Error: Opndlg

Correct: OpenDialog

Third, the name

A. Java Naming conventions

The naming should conform to the camel- like rules, alternating between uppercase and lowercase letters to differentiate between different words in the name, i.e. lower case letters, but with the class name, interface name, and the first letter of any non-initial word capitalized.

1. Use the full English descriptor as much as possible, using the terminology applicable to the field

2. Use case mix to make name readable

3. Use abbreviations sparingly, but if used wisely.

4. Avoid using a similar name, or just a different name for the case

Type

Rules

Example

Package Name

All lowercase letters, URL reverse

Package Com.domain.project

Class name

Capitalize the first letter, use a descriptive noun, avoid abbreviations unless the abbreviation is very generic (URL, Html)

Class Raster;

Class Imagesprite;

Interface

Ditto. Usually begins with the letter I. Callback interface and other special cases can not add I.

Interface Irasterdelegate;

Interface istoring;

Interface Onclicklistener;

Method

Use a verb, hump-named, first letter lowercase

Run ();

Runfast ();

Getbackground ();

Variable

Use meaningful short words, hump-type, first letter lowercase.

The member variable m begins with the JavaBean exception.

The static variable s begins.

int i;

float Mywidth;

int mprivate;

Static MyClass Ssingleton;

Constant

All caps, dividing words with underscores, static final decoration

static final int min_width = 4;

B. Android Naming conventions

Type

Rules

Example

Activity

Xxxxactivity

Mainactivity

View

Xxxview,xxxlayout

Redtextview, Bluetoothdialog

Service

Xxxservice

Broadcastreceiver

Xxxxreceiver

Tool Method Classes

Utils or manager is a suffix

Threadpoolmanager, Logutils

Basic class

Basexxx

Baseactivity, Basefragment, Basedao

Layout xml

All lowercase, underlined, and named using nouns. The layout file for activity or fragment must correspond to its class name, with the following rule: convert all letters to lowercase and swap types and functions.

Activity_main.xml

Fragment_homework.xml

Dialog_bluetooth.xml//dialog box

Item_message.xml//list item

Vw_titlebar.xml//Other layout files

Layout ID

All lowercase, underline split, view abbreviation _view logical name

Layout_city, Tv_name, Btn_submit, Img_head, List_message

Picture file

All lowercase, with the underscore split.

Xxxx_checked.png

Xxxx_focused.png

Xxxx_selected.png

Xxxx_pressed.png

Xxxx_disabled.png

Xxxx_normal.png

drawable XML

Selector_xxx, Shape_xxx


Iv. notes

1. Each class must have a file header comment. Briefly describe the role of the class, stating the author and creation time. Standard templates:

/**

* FileDescription.

*

* @author ${user}

* @date ${date}

*/

2. Most methods require methodological comments, with the exception of some self-explanatory methods. Briefly describes how the method works and interprets parameters, return values, and throws exceptions. For method comments, use the Javadoc standard. For example:

/**

* Description.

*

* @param arg1 Description

* @param arg2 Description

* @return Description

* @throws Exception Description

*/

public int Getfoo (int arg1, booleanarg2) throws Exception {

return 0;

}

3. Critical logic or more complex logic, you should add the necessary annotations. Single-line comments use "//", multiline comments using/**/.

4. Comments must be updated in real time when the program changes.

5. Simple and straightforward, to ensure that any programmer can read.

V. OTHER SPECIFICATIONS

1. All file encoding format is UTF-8.

2. The scope of the variable should be as small as possible, declared when needed, and initialized as soon as possible.

3. Do not omit a single line of curly braces, and do not leave out the curly braces for single-line code blocks.

That's right:

if (condition) {

statements;

}

Error:

if (condition)//No curly braces

Statement

Error:

if (condition)//Front curly brace takes a single row

{

Statement

}

4. Use abbreviations sparingly, unless the abbreviation is common (Html, URL)

5. Indent with 4 spaces instead of tab.

6. You must format the code after writing it. Androidstudio default formatting shortcut key: Ctrl+alt+l.

7. At the beginning and end of the logical code block in your code, you should add a blank line and write a comment at the beginning.

A blank line must be added between the relatively independent blocks.

8. A line of code does not exceed 100 characters. More than 100 characters, please wrap or extract variables.

9. The method body does not exceed 40 lines. If it is exceeded, you should consider splitting it into multiple methods.

10. Avoid using enumeration classes and use constants instead.

11. Use @override annotations as long as they are legal.

12. Standardize todo usage, add dates and descriptions, and resolve deletions in a timely manner. Androidstudio can tap Todo to use the standard template. For example:

TODO:2017/1/5 Simple Description

13. Use a unified logutils to output log. The output log should be concise. A sufficient log can be used to locate errors, but too much log will affect program performance (I/O time). The official version should turn off the log switch.

Unnecessary import is not allowed in Java files and Importjava.io is not allowed. *

Warning to be resolved.

16. Hard coding is not allowed, and the status value must define constants and add comments.

Configure Codestyle

Android Studio Unified Configuration

1. Configure Codestyle

Import the Androidcodestyle.xml file in order of labeling.

2. Project Document coding Unified for UTF-8

3. Note Templates

Note: Modify the ${user} to its own name, with no extra spaces or newline characters before or after the template.

/**

* File description.

*

* @author ${user}

* @date ${date}

*/

4. Install using the Checkstyle plugin

After the installation is complete, restart the Androidstudio. Then add the Checkstyle script:

Configuring real-time checking

Introduction to How to use:

-After a real-time check is configured, in the editor, the code that does not conform to the style directly appears with a warning:

-Right-click on the options in the Java file to view all the non-conforming code snippets for the current file:

Android code specification and studio configuration Codestyle

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.