Cách viết code và hiển thị chuẩn cho Java sử dụng nguồn từ trang raywenderlich.
Hiện tại có rất nhiều các nguồn khác nhau đưa ra các style để viết code cho java cũng như Android, mình có thể thì đang tham khảo 2 nguồn chính tại Android contributors style guide và Google Java Style Guide.
- Nomenclature - Cách đặt tên
- Declarations - Khai báo
- Getters & Setters
- Brace Style - Ngoặc nhọn
- Switch Statements
- Annotations
- XML Guidance
- Language
Cách đặt tên cơ bản phải theo quy định của java và xml
Tất cả text đều phải là chữ thường (lower-case), nếu mà có nhiều từ khác trong một package thì viết liền không luôn các từ đó lại với nhau:
BAD:
com.omiNext.packages_nameGOOD:
com.ominext.packagesnameViết bằng UpperCamelCase (Viết hoa chữ cái đầu của từng từ). For example OldDog.
Viết bằng lowerCamelCase (Viết hoa chứ cái đầu của từng từ trừ chữ đầu). For example setColor.
Viết bằng lowerCamelCase.
Riêng các biến static final thì viết bằng uppercase, và phân biệt các từ bằng dấu gạch dưới:
public static final int THE_AGE = 25;- Các biến Non-public, non-static thêm
mvào trước. - Các biến static thì thêm
svào trước.
For example:
public class MyClass {
public static final int SOME_CONSTANT = 42;
public int publicField;
private static MyClass sInstance;
int mPackagePrivate;
private int mPrivate;
protected int mProtected;
}Viết bằng lowerCamelCase.
Những từ viết tắt thì mình chỉ viết hoa chữ cái đầu, các chữ cái viết tắt sau thì mình viết thường. For example:
BAD:
XMLHTTPRequest
String URL
findPostByIDGOOD:
XmlHttpRequest
String url
findPostByIdCần phân quyền rõ ràng cho các lớp, biến, hàm. (public, private, protected...)
Hãy khai báo mỗi biến một dòng để dễ nhìn và dễ quản lý.
BAD:
String username, twitterHandle;GOOD:
String username;
String twitterHandle;Nên viết mỗi class trong một file.
Nếu khai báo một enum class mà không tồn tại hàm trong đó thì các enum viết trên một dòng.
private enum CompassDirection { EAST, NORTH, WEST, SOUTH }Để truy vấn các biến private hoặc protected cần tạo get và set cho các biến đó
Tham khảo của Google: http://developer.android.com/training/articles/perf-tips.html#GettersSetters
Mình nên để dấu mở ngoặc nhọn trên cùng dòng với dòng định nghĩa tên hàm
BAD:
class MyClass
{
void doSomething()
{
if (someTest)
{
// ...
}
else
{
// ...
}
}
}GOOD:
class MyClass {
void doSomething() {
if (someTest) {
// ...
} else {
// ...
}
}
}Trong các function điều kiện thì phải đưa các câu lệnh vào trong dấu {} dù có chỉ có 1 lênh.
BAD:
if (someTest)
doSomething();
if (someTest) doSomethingElse();GOOD:
if (someTest) {
doSomething();
}
if (someTest) { doSomethingElse(); }Sử dụng switch, bắt buộc phải có default cuối switch
BAD:
switch (anInput) {
case 1:
doSomethingForCaseOne();
case 2:
doSomethingForCaseOneOrTwo();
break;
case 3:
doSomethingForCaseOneOrThree();
break;
}GOOD:
switch (anInput) {
case 1:
doSomethingForCaseOne();
// fall through
case 2:
doSomethingForCaseOneOrTwo();
break;
case 3:
doSomethingForCaseOneOrThree();
break;
default:
break;
}Chú thích cho phần Override Interfaces @Override cần được viết trên function một dòng. (Không nên bỏ đi)
BAD:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}GOOD:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}Các rules cho file XML view android
Phải viết loại view của file trước rồi viết tên của các file sau. Tất cả các chữ viết thường, các từ cách nhau bằng dấu gạch dưới.
BAD:
login.xmlmain_screen.xmlrounded_edges_button.xml
GOOD:
activity_login.xmlfragment_main_screen.xmlbutton_rounded_edges.xml
XML resources phân chia theo cấu trúc của Android bên dưới:
- Strings =>
res/values/strings.xml,res/values-vi/strings.xml - Styles =>
res/values/styles.xml,res/values-sw620dp/styles - Colors =>
res/color/colors.xml - Animations =>
res/anim/ - Drawable =>
res/drawable,res/drawable-xhdpi - Layout =>
res/layout,res/layout-xlarge
Use US English spelling.
BAD:
String colour = "red";GOOD:
String color = "red";The following copyright statement should be included at the top of every source file:
/*
* Copyright (c) 2016 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/