Android/java Code Specification __java

Source: Internet
Author: User
Tags lowercase checkstyle

As the company's most recent development personnel, as a company responsible for development, must do code controllability, summed up the previous development experience, designed a set of development code specifications, was originally given to the internal use. Now here's the price to share.

The company's internal norms are mainly used Checkstyle, FindBugs + Jenkins to do control.

After running, only to find that their writing code a variety of worrying.

The specific specifications are as follows: Android/java code Specification I, document history

version Description Date author
1.0 Java Code Specification Draft 2014-7-1 Zhou San
2.0 Cheng One Java specification development 2016-7-25 Zhou San
Ii. Introduction

A good code specification reduces communication costs, refactoring costs, and maintenance costs, reducing the likelihood of bugs appearing.
This paper describes the Java code specification for the Prudential One Financial Java project and the Android project.

Description

"Do" is to be obeyed. 2.1 environment settings in an automated format

Using the eclipse or idea environment for development, you need to import the automatic format configuration file. With the Checkstyle plug-in configuration file. 2.1.1

Formatting
The Automation Format environment directory settings for eclipse use the following files. Checkstyle_config_chengyiwm.xml eclipse_code_formatter.xml eclipse_code_templates_chengyi_java.xml 2.2 automatic Check Plugin checkstyle:http://eclipse-cs.sourceforge.net/update Findbugs:http://findbugs.cs.umd.edu/eclipse

Http://chengyiwm-wordpress.stor.sinaapp.com/uploads/2016/08/ci.zip ' > Click to download: Code formatting, checkstyle configuration file Android Java code Specification Three, document format

Most of the requirements for this document format and annotations can be completed in Eclipse's automated format. 3.1 Typesetting specification source file encoding do 1. All source files are encoded as UTF-8 do 2. Indent to 4 spaces , (not tab-critical) do 3. No more than 200 characters per line, more than the need to break a line of space, blank lines

variables, operators, parameters must be added between the spaces

Bad Example: (operator ' if ', ' = ' without space before and after)

        Profile Profile=getuserinfo (Param.getreqhead (). GetUserID ());
        Getuserinforesp resp=new Getuserinforesp ();
        if (profile!=null) {
            beanutils.copyproperties (profile, resp);
        }
        Resp.setusertype (Authinfoservice.getauthstatus (Param.getreqhead (). GetUserID ()));
        Return resp;

Bad Example: (curly braces do not wrap)

        if (profile!=null) {beanutils.copyproperties (profile, resp);}
attribute Order Do 4. The order of the properties of the method is public, protected, private Curly Braces do 5. The opening brace does not occupy a single line, and the closing brace is independently lined

Bad Example:

if (condition)
{
//do something
}

That's right:

if (condition) {
    //do something
}
Do 6. You cannot omit curly braces if there is only one line in the code block. such as:

Bad Example:

if (Gpsfightinfo = = null)
showloadingscreen (screen_action_resume, 0, 1.0f);
else
Showloadingscreen (screen_action_resume, 0, 0.5f);

for (int i = 0; i < headers.length i++)
headers[i] = Headers[i].tolowercase ();

That's right:

if (Gpsfightinfo = = null) {
showloadingscreen (screen_action_resume, 0, 1.0f);
} else {
showloadingscreen ( Screen_action_resume, 0, 0.5f);
}           
for (int i = 0; i < headers.length i++) {
Headers[i] = Headers[i].tolowercase ();
}
3.2 Notes Do 7. Use template with standard Javadoc (CTRL+ALT+J)
Example:
    /**
     * Add address
     * @param addressparam address parameter
     * @return address request return result
     * @throws ioexception IO read/write error/
    public Object addaddress (Addaddressparam addressparam) throws IOException {return
        addressservice.addaddress (param);
    }

Bad Example: (don't use meaningless automatic annotation @description, @param @param param don't know what it means ...)

    /** *
     @throws ioexception * 
     @throws nosuchalgorithmexception 
     * @throws keymanagementexception 
     * 
     * @Description: User Registration
     * @author wangshuxiong@chengyiwm.com
     * @param @param param 
     * @param @return C18/>* @return Object
     * @date April 21, 2016 afternoon 1:12:28
     * @throws * * Public
    Object updateaddress ( Updateaddressparam param) throws Keymanagementexception, NoSuchAlgorithmException, IOException
/class/interface/enum for do 8.public needs comment Field/method for do 9.public needs comment

Must be written on it, not wrapped.

Bad Example:

    Private Integer optype;//0 new 1 modified

Bad Example 2:

    0 New 1 Modify
    private Integer optype;

That's right:

    /** Operation State 0 New 1 Modified * *
    private Integer optype;
Four, naming

Do not underestimate naming, improper naming will bring great reading barriers, but also increase the later reconstruction and maintenance costs. 4.1 The base naming specification do 10.package\class is named lowercase with no dash and underline

Example:

Com.chengyiwm.goldman
name of Do 11.class/interface/enum using Pascalcase

Example:

Assetmanager
Screen
screentype
name of Do 12.method using CamelCase

Example:

public void Loadessentialassets ();
Do 13.private or protected field uses the "M" prefix (the meaning of M is member), and the underlined part uses CamelCase

Example:

Private Assetmanager Massetmanager;
Do 14.static field uses the G prefix (the meaning of G is global), and the part after the underline uses CamelCase

Example:

private static class[] Gclassarray;
Do 15.constants is named Full_upper_case and the word is separated by an underscore

Example:

public static final int access_random = 1;
public static final String Font_hp_path = "Fonts/hp.fnt";
The fields in Do 16.enum are named Pascalcase, and each field is separated by rows
Example:
Public enum Element {Earth
,
Hero,
    WaterFire
}
Recommendation: Name must be self explanatory, meaningful, do not use I, J, K name

Bad Example:
for (int i = 0; i < J; i++) {
byte byte0 = md[i];
str[k++] = hexdigits[byte0 >>> 4 & 0xf];
str[k++] = hexdigits[byte0 & 0xf];
}

Name must use reasonable, commonly used English words, or industry terminology, to eliminate the use of unpopular words do 17 abbreviations must refer to the abbreviation Specification table, not in the abbreviation specification list does not use do 18.Method name need to be dosomething Do 19.Class name needs to be namephrase (first letter capital) do 20. Data types do not need prefix, Hungarian nomenclature Common resource naming Do 21.Activity name: xxxxactivity Do 22.View name: Xxxxview or Xxxxlayout Do 23.Service name: Xxxxservice Do 24.BroadcastReceiver name: Xxxxreceiver Do 25.Layout file name:
The content view of the activity should be used: the form of activity_xxxx.xml
Custom view should use: View_xxxx.xml resource file naming do 26. About statelistdrawable named suffixes for various states

File name All lowercase, the specific role and state of the button, the word between the use of underline connection.

Bad Example:

Click_button.png
clickbutton.png ...
..

That's right:

    Login_normal.png
    login_pressed.png
    login_focused.png
    xxxx_checked.png
    xxxx_focused.png
    Xxxx_selected.png
    xxxx_pressed.png
    xxxx_disabled.png
    xxxx_normal.png
name of Do 27. Constant file
strings.xml//all string resource files need to be declared here
dimens.xml
drawables.xml
styles,xml//generic style style needs to be
written again Themes.xml
colors.xml
ids.xml
Do 28. Third-party plug-ins, you need to add a prefix, with local resources to distinguish
Example:
That's right:
Couba plugin logo:xueba_logo.png
graphics plugin logo:wantu_logo.png
error:
Couba plug-in logo:logo.png
play map plugin Logo:logo.png

/*
* @author Zhoushengtao (Zhou San)
* @since July 16, 2016 16:47:20
* @weixin stchou_zst
* @blog http://blog.csdn.net/yzzst
/

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.