Download Conding Convention for Android_v1.0

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Conding Convention
For Android
Conductor: ThuongNV
Version: 1.0
Reference:
• Java Coding convention of Fsoft
• http://developer.android.com/guide/index.html
Content
• Standard Java coding convention
• Icon design guidelines on Android
• Designing for Performance on Android
Standard Java Coding convention
 General rule
 Naming convention
 Java source files
 Comment code
 Declarations
General rule
 Simple
 Precise
Naming convention
 Names should be functionally meaningful.
 Identifiers must be as short as possible (<=20 character).
 Avoid names that are similar or differ only in case.
Ex: persistentObject and persistentObjects
 Avoid cryptic names
 Abbreviations in names should be avoided.
computeAverage(); // NOT: compAvg();
generateHTML();
// NOT: generateHypertextMarkupLanguage();
 The method name should not contain any special
characters other than underscore (_).
Naming convention
 Class/Interface name must start with uppercase
 Variable name must start with lowercase
 Package name must be lowercase.

Ex: com.fsoft.atravel.activity
 Constants:


All constants to be named in uppercase letters, with underscore
between words.
All constants must be declared as static final.
 Method:


Method name must start with lowercase letter.
Usually use “active verb” as the first word of method name
isActive, hasAddresses
 getNewData, showMap

Java source file
 Java source files have the following ordering:
 Beginning comments
 Package and Import statements
 Class and interface declarations
Java source file example
/*
* Classname
*
* Version information
*
* Date
*
* Copyright notice
*
* Modification Logs:
* DATE
AUTHOR DESCRIPTION
* -------------------------------------------------------* 10-Aug-2003 CuongDD Description of modification
*/
package com.fsdn.atravel.activity;
import java.util.ArrayList;
public class ClassName{
// class content
}
Comment code
 Block Comments
/*
* Here is a block comment.
*/
 Single-Line Comments
if (condition) {
// Handle the condition.
...
}
 Trailing Comments
if (a == 2) {
return TRUE;
}
// special case
 End-Of-Line Comments
//if (bar > 1) {
// // Do something.
Declaration
 One declaration per line is recommended since it
encourages commenting
int level; // indentation level
int size; // size of table
 Array Declaration
int anIntArray[]; // AVOID
int[] anIntArray; // RECOMMENDED
 Initialize local variables where they're declared
 Using try-catch statement
Declaration
 Braces “{}” are used around all statements, even single
statements, when they are part of a control structure,
such as:



if-else
for statement
while statement
if (condition) //AVOID! THIS OMITS THE BRACES {}!
statement;
if (condition) { // RECOMMENDED
statement;
}
Content
• Standard Java coding convention
• Icon design guidelines on Android
• Designing for Performance on Android
Icon design guideline
 Using the Android Icon Templates Pack
 Providing Density-Specific Icon Sets
Icon design guideline(cont.)
 Use common naming conventions for icon assets
Icon design guideline (cont.)
 Set up a working space that organizes files for
multiple densities
assets/...
ldpi/...
_pre_production/...
working_file.psd
finished_asset.png
mdpi/...
_pre_production/...
working_file.psd
finished_asset.png
hdpi/...
_pre_production/...
working_file.psd
finished_asset.png
res/...
drawable-ldpi/...
finished_asset.png
drawable-mdpi/...
finished_asset.png
drawable-hdpi/...
finished_asset.png
Icon design guideline (cont.)
 When saving image assets, remove unnecessary
metadata:
Ex: To remove the Photoshop header, follow these
steps:



Under the File menu, choose the Save for Web & Devices
command
On the "Save for Web & Devices" panel, set the Preset pop-up
to "PNG-24," set the pop-up under Presets to "PNG-24" as
well, and select the Transparency box (if the image uses
transparency)
Select Save.
Content
• Standard Java coding convention
• Icon design guidelines on Android
• Designing for Performance on Android
Designing for Performance
 Avoid Creating Objects
 Example 1:
System.out.println(txtEmail.getText().toString());
 String temp = txtEmail.getText().toString();
System.out.println(temp);


Example 2: Slice up multidimensional arrays into parallel
single one-dimension arrays.
An array of ints is a much better than an array of Integers.
 Two parallel arrays of ints are also a lot more efficient than an
array of (int,int) objects.

Designing for Performance(cont.)
 Avoid internal getters/setters
 Use static final for constants
 Use "for-each" loop syntax. For example:
static class Foo {
int mSplat;
}
Foo[] mArray = …
Slowest:
public void zero() {
int sum = 0;
for (int i = 0; i < mArray.length; ++i) {
sum += mArray[i].mSplat;
}
}
Fastest:
public void two() {
int sum = 0;
for (Foo a : mArray) {
sum += a.mSplat;
}
}
Question & Answer
Related documents