Android coding specifications and Code style

Source: Internet
Author: User
Tags constant definition

I. Common naming conventions

1. Pascal's method of life:

Capitalize the first letter of each word. such as: PersonName, Schoolname

2. Camel Name Law:

The first word is lowercase and the first letter of the remaining words is capitalized. such as: PersonName, Schoolname

3. Hungarian nomenclature:

Variable name = attribute + Type + object description. such as: M_intmaxdate

Properties: Global Variables (g_), Constants (C_), member variables (m_), static variables (s_)

Type: Function (FN), handle (hand), long Integer (Long), Boolean (bool), float (float), double word (DW), String (str), double-precision floating point (double), count (count), character (char), integer (int), BYTE (byte)

Description: Max (max), min (min), initialization (Init), temporary variable (t/temp), source object (SRC), Destination object (Dest)

4, underline the name method:

All letters are lowercase, with an underscore split between each word. such as: Person_name

Second, the use of naming norms

1. Package name all lowercase packages Istg.tolo.common

2, file naming (ie, class name) Pascal named

(1) General class (Pascal nomenclature)

public class someclass{

Public SomeMethod () {}//constructor

}

(2) custom attribute class (suffix using attribute)

public class Bussinessplugauthorattribute extends attribute{...}

(3) Custom exception class (suffix using exception)

public class Bussinessexception extends exception{...}

(4) Type pass-through in generics

public class linkedlist<k,t>{...}

3. Method name (verb phrases , such as: Eatfood, not food)

public class someclass{

Public SomeClass () {}; Pascal named for constructors

public void Someclassmethod () {...}; Camel named for normal functions

}

4, interface naming (suffix naming method)

interface file and name definition: Review, Reviewlistener

Event Interface Definition: Onclicklistener

5. Variable naming

Private member variable (class variable): prefix m+pascal nomenclature, such as: Private String Mmyname

Private member variable (class variable) (optimization): Camel naming method, such as: Private String MyName

Static member variables: prefix s+pascal nomenclature, such as: Static String Smyname

Local variables: Camel nomenclature, such as: String resolvedtype = null;

Local variables (Optimizations): prefix _+pascal nomenclature, such as: String _resovledtype = null;

Method internal parameters: Camel naming method, such as: Eatfood (intentsender intent, int requestcode) {...}

Method internal parameters (optimization): prefix p+pascal nomenclature, such as: Eatfood (intentsender pintent, int prequestcode) {...}

Variables in the loop: You can use abbreviated letters, meaningless letters, such as: for (i=0;i<count;i++) {...}

Third, the name of the format

1, Strict line change

A; a line-break

2, Strict indentation

Use the tab indent

Annotation indentation and Code indent alignment

3. Strict annotation

File, class, method, method function, variable, attribute, all to be commented

Comments for files and classes must include the following elements: Name, Description, Version, Date, Author, change, Copyright

Follow Javadoc standards, such as:

/**

*

*/

This is my name.

Private String Mname;

4, the location of the declaration

(1) A set of namespaces defined by all frameworks, custom and third-party namespaces placed in another group

(2) Class member variable declaration location

All member variables should be declared at the top, using a blank line to separate them from the property methods. Such as:

public class myclass{

int mnumber;

String Mname;

public void SomeMethod () {...}

}

(3) local variable declaration location

Local variables are placed at the top of the method

5. Declaring variables using Java predefined types without using the class name

Use predefined types (keywords) without using the class name to declare variables, such as:

Use object instead of object

Use int instead of Int32

Iv. code Examples of specifications

 /*** Student Management activity class *@authorJiatao * @date 2015-4-25 * @company zkr * */ Packagemobidever.demo.students;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.Toast;/*** Student Management class *@authorJiatao * @date 2015-4-25 *@sinceLevel 8 (Android 2.0) *@version1.0*/ Public classStudentmanageactivityextendsActivity {//defined student Private object PrivateObject mstudent;//static definition of student variables Private StaticObject sstudent;/*** Constant definition of Class_one class*/ Private FinalString Class_one = "Class1"; Private FinalString Message_warn = "The content you entered is not correct, please re-enter"; /*** Get student information *@returnGet student Information*/  PublicObject getmstudent () {returnmstudent;} /*** Set up student information *@parammstudent specific student information values*/  Public voidsetmstudent (Object mstudent) { This. mstudent =mstudent;} @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub  Super. OnCreate (savedinstancestate); //functions that invoke the initialization interfaceInitdisplay (); //Call class student calculation function to get the number of students in Class1 class   This. Calculatestudentscount (Class_one); Toast.maketext ( This, Message_warn, 3000). Show (); }   /*** Number of students per class *@paramPclassname class name *@returnreturn class number of students *@authorJiatao * @date 2015-4-25 *@version1.0 * * Added to the judgment of male and female students *@authorxuexing * @date 2015-4-26 *@version1.1*/  Public intCalculatestudentscount (String pclassname) {int_studentcount = 5;//local variables within a method//Pstudentcount = 12; //parameter variables within a method//mstudentcount = 13; //class member Variable  return_studentcount; }   /*** Initialization Interface*/  Public voidInitdisplay () {}}

Android coding specifications and Code style

Related Article

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.