轉載請把頭部出處鏈接和尾部2維碼1起轉載,本文出自逆流的魚yuiop:http://blog.csdn.net/hejjunlin/article/details/52614696
前言:Android中1些開發規范,避免給自己和他人少留坑。
2.2.15 Field Ordering 屬性排序
在類文件頂部聲明的任何屬性都應當按以下的排序規則進行排序:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public static enum {
ENUM_ONE, ENUM_TWO
}
public static final String KEY_NAME = "KEY_NAME";
public static final int COUNT_USER = 0;
@Inject SomeAdapter mSomeAdapter;
@BindView(R.id.text_name) TextView mNameText;
@BindView(R.id.image_photo) ImageView mPhotoImage;
private int mUserCount;
private String mErrorMessage;
public int mSomeCount;
public String mSomeString;
使用上述的排序規則有助于保持字段聲明的分組,從而增加字段的可讀性
2.2.16 Class member ordering 類成員排序
為了提高代碼的可讀性,組織類成員在1個符合邏輯的方式中是非常的重要,請按以下的排序方式去實現:
例如:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public class MainActivity extends Activity {
private int mStepCount;
public static newInstance() { }
@Override
public void onCreate() { }
public void setColor(Color color) { }
private int getId() { }
static class AnInnerClass { }
interface SomeInterface { }
}
在Android框架中任何生命周期的方法應當在其相應的生命周期中排序,例如:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public class MainActivity extends Activity {
// Field and constructors
@Override
public void onCreate() { }
@Override
public void onStart() { }
@Override
public void onResume() { }
@Override
public void onPause() { }
@Override
public void onStop() { }
@Override
public void onRestart() { }
@Override
public void onDestroy() { }
// public methods, private methods, inner classes and interfaces
}
2.2.17 Method parameter ordering 方法的參數排序
當定義方法時,參數應當依照以下的規則排序:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public Post loadPost(Context context, int postId);
public void loadPost(Context context, int postId, Callback callback);
Context上下文參數應放在第1位,并且Callback回調參數放置在最后
2.2.18 String constants, naming, and values 字符串常量、命名和值
當使用字符串常量時,其應當修飾為靜態final并且遵守以下規則:
2.2.19 Enums 枚舉
枚舉的使用僅僅在實際需要用到時。如果另外1種方法可行,此時應當選擇更好的方式去實現它,例如:
相對下面這樣:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public enum SomeEnum {
ONE, TWO, THREE
}
更推薦這樣做:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
private static final int VALUE_ONE = 1;
private static final int VALUE_TWO = 2;
private static final int VALUE_THREE = 3;
2.2.20 Arguments in fragments and activities
在fragment和activity中的參數
當我們使用Intent或Bundle傳遞數據時,值的鍵必須使用下面定義的約定:
當創建fragment或activity的新實例觸及到傳遞數據時,我們應當提供1個靜態的方法來獲得新的實例,傳遞的數據作為參數。例如:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
//Activity中
public static Intent getStartIntent(Context context, Post post) {
Intent intent = new Intent(context, CurrentActivity.class);
intent.putParcelableExtra(EXTRA_POST, post);
return intent;
}
//Fragment中
public static PostFragment newInstance(Post post) {
PostFragment fragment = new PostFragment();
Bundle args = new Bundle();
args.putParcelable(ARGUMENT_POST, post);
fragment.setArguments(args)
return fragment;
}
2.2.21 Line Length Limit 行長度限制
代碼的行數字符長度最好不要超過100個字符,這樣代碼的可讀性會更高。有時為了實現上述要求,我們需要做的:
2.2.21.1 Line-wrapping techniques 換行技能
當觸及到換行時,有1些情況我們應當保持與格式化代碼的1致性。
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
int count = countOne + countTwo - countThree + countFour * countFive - countSix
+ countOnANewLineBecauseItsTooLong;
如果需要,你可以直接在“=”后換行:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
int count =
countOne + countTwo - countThree + countFour * countFive + countSix;
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
Picasso.with(context).load("someUrl").into(imageView);
取而代之應這樣:
Picasso.with(context)
.load("someUrl")
.into(imageView);
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
private void someMethod(Context context,
String someLongStringName,
String text,long
thisIsALong, String anotherString) {
}
當調用這個方法時,我們應當在每一個參數的逗號后面換行:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
someMethod(context,
"thisIsSomeLongTextItsQuiteLongIsntIt",
"someText",
01223892365463456,
"thisIsSomeLongTextItsQuiteLongIsntIt");
2.2.22 Method spacing(方法間間距)
在同1個類中,方法與方法之間只需要留有1行的空白,以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public String getUserName() {
// Code
}
public void setUserName(String name) {
// Code
}
public boolean isUserSignedIn() {
// Code
}
2.2.23 Comments(注釋)
2.2.23.1 Inline comments(行內注釋)
必要的時候,寫注釋,其他情況下最好不要寫注釋,從方法名或成員變量上就可以看出做甚么。
2.2.23.2 JavaDoc Style Comments(java文檔的注釋風格)
方法的名字應當起的和該方法的功能相對應,有時可以提供JavaDoc風格的注釋。方法名起的好會幫助讀者更好的理解方法的功能,同時也會讓使用者明白傳入方法中參數的作用。
/**
* Authenticates the user against the API given a User id.
* If successful, this returns a success result
*
* @param userId The user id of the user that is to be authenticated.
*/
public class XXX {
}
/**
* RecyclerView adapter to display a list of {@link Post}.
* Currently used with {@link PostRecycler} to show the list of Post items.
*/
public class RecyclerView {
}
不要寫初創作者信息,由于以后會有很多人在這個類上改來改去,寫上作者信息是沒有任何意義的。
/**
* Created By yuiop 22/09/2016
*/
public class XXX {
}
2.2.24 Sectioning code(分段代碼)
2.2.24.1 Java code(java代碼)
如果對代碼做了“分段”,應當使用下面的方法完成,以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public void method() { }
public void someOtherMethod() { }
/********* MVP Method Implementations ********/
public void anotherMethod() { }
/********* Helper Methods ********/
public void someMethod() { }
不能像下面這樣:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
public void method() { }
public void someOtherMethod() { }
// Mvp Method Implementations
public void anotherMethod() { }
這樣會更容易定位類中方法。
2.2.24.2 Strings file(字符串文件)
字符串資源文件string.xml中分段注釋以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
// User Profile Activity
<string name="button_save">Save</string>
<string name="button_cancel">Cancel</string>
// Settings Activity
<string name="message_instructions">...</string>
這樣寫不但可讓string文件看起來整潔,還能在需要更改它們時更容易找到。
2.2.24.3 RxJava chaining(RxJava鏈接)
當進行異步操作時,每步操作都應當在遇到“.”號之前另起1行,以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
return dataManager.getPost()
.concatMap(new Func1<Post, Observable<? extends Post>>() {
@Override
public Observable<? extends Post> call(Post post) {
return mRetrofitService.getPost(post.id);
}
})
.retry(new Func2<Integer, Throwable, Boolean>() {
@Override
public Boolean call(Integer numRetries, Throwable throwable) {
return throwable instanceof RetrofitError;
}
});
這樣會使讀者更容易理解接下來的異步操作。
2.2.25 Butterknife(Butterknife)
2.2.25.1 Event listeners(事件監聽者)
如有可能,盡可能使用ButterKnife綁定監聽。舉個栗子,可以用ButterKnife替換傳統的點擊事件:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
mSubmitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Some code here...
}
};
換成以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
@OnClick(R.id.button_submit)
public void onSubmitButtonClick() { }
2.3 XML Style Rules(XML文件中樣式規則)
2.3.1 Use self=-closing tags(使用單標記)
在xml布局中,如果1個viwe沒有任何子view,那末就應當使用單標記。
用這個:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
<ImageView
android:id="@+id/image_user"
android:layout_width="90dp"
android:layout_height="90dp" />
不用這個:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
<ImageView
android:id="@+id/image_user"
android:layout_width="90dp"
android:layout_height="90dp">
</ImageView>
2.3.2 Resource naming(資源命名)
所有的資源命名規則都應當是小寫和下劃線的組合,以下:
2.3.2.1 ID naming(id命名)
所有的id命名規則都應當用元素作為前綴。
Element | Prefix |
---|---|
ImageView | image_ |
Fragment | fragment_ |
RelativeLayout | layout_ |
Button | button_ |
TextView | text_ |
View | view_ |
例如:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
<TextView
android:id="@+id/text_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
注意:如果1個布局中1種類型的view只有1種,比方toolbar,那末可以直接起名叫toolbar
2.3.2.2 Strings(字符串)
所有的字符串名字應當以該利用確當前功能頁面作為前綴,以下:
Screen | String | ResourceName |
---|---|---|
Registration Fragment | “Register now” | registration_register_now |
Sign Up Activity | “Cancel” | sign_up_cancel |
Rate App Dialog | “No thanks” | rate_app_no_thanks |
如果沒法像上面1樣命名,我們可以用下面的方法:
Prefix | Description |
---|---|
error_ | Used for error messages |
title_ | Used for dialog titles |
action_ | Used for option menu actions |
msg_ | Used for generic message such as in a dialog |
label_ | Used for activity labels |
需要注意以下兩點:
2.3.2.3 Styles and themes
當定義style和theme時,每一個單詞應當大寫開頭。以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
AppTheme.DarkBackground.NoActionBar
AppTheme.LightBackground.TransparentStatusBar
ProfileButtonStyle
TitleTextStyle
2.3.3 Attributes ordering(屬性排序)
定義屬性不能只為了看起來整潔,同時能夠在布局中快速找到屬性位置。以下是基本規則:
以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
<Button
android:id="@id/button_accept"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:padding="16dp"
android:text="@string/button_skip_sign_in"
android:textColor="@color/bluish_gray" />
注意:在Android studio中快速格式化快捷鍵是:cmd + shift + L
這樣做,當布局文件產生變化時,可以通過xml屬性快速定位。
本文出自逆流的魚yuiop:http://blog.csdn.net/hejjunlin/article/details/52614696
2.4 Tests style rules(測試風格規則)
2.4.1 Unit tests(單元測試)
所有測試類起名字都應當和他們被測試的類相對應,并且以Test作為后綴,以下:
Class | Test Class |
---|---|
DataManager | DataManagerTest |
UserProfilePresenter | UserProfilePresenterTest |
PreferencesHelper | PreferencesHelperTest |
所有的測試方法應當用@Test進行注釋,測試方法應當用下面的模板:
@Test
public void methodNamePreconditionExpectedResult() { }
舉例,如果我們想測試1個使用不正確郵箱登錄的功能,測試方法應當使用以下的:
@Test
public void signUpWithInvalidEmailFails() { }
測試應當將重點放在測試方法賦予的功能名稱上面,如果在你的測試方法中還有別的情況需要斟酌,這些額外的需要測試的情況應當分到它專門的測試方法中。
如果1個類中包括許多不同的方法,測試應當在多個測試類中進行拆分-這樣有助于測試更容易于保護和定位。例如,1個數據庫工具類有時候會分解成以下幾個測試類:
DatabaseHelperUserTest
DatabaseHelperPostsTest
DatabaseHelperDraftsTest
2.4.2 Espresso tests(功能測試框架Espresso)
每一個Espresso測試類1般都對應1個Activity,所以命名時應當和對應的Activity相1致,其次是測試,以下:
Class | Test Class |
---|---|
MainActivity | MainActivityTest |
ProfileActivity | ProfileActivityTest |
DraftsActivity | DraftsActivityTest |
當使用Espresso API的時候,方法應當換行從而可讓聲明更容易讀,舉例以下:
onView(withId(R.id.text_title))
.perform(scrollTo())
.check(matches(isDisplayed()))
這類風格的鏈接調用不但可讓我們每行不超過100個字符,同時也能夠讓Espresso測試中的鏈接更加易讀。
3.1.1 Versioning
如果1個版本號在多個依賴中多被使用,那末應當在依賴的范圍內定義成1個變量,以下:
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
final SUPPORT_LIBRARY_VERSION = '23.4.0'
compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:percent:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:customtabs:$SUPPORT_LIBRARY_VERSION"
將來如果需要更新依賴,那末只需要更新版本號的變量就能夠很輕松的控制所有依賴的版本號。
3.1.2 Grouping(分組)
//create by 逆流的魚yuiop on 2016/9/22
//blog地址:http://blog.csdn.net/hejjunlin
compile "com.android.support:percent:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:customtabs:$SUPPORT_LIBRARY_VERSION"
compile 'io.reactivex:rxandroid:1.2.0'
compile 'io.reactivex:rxjava:1.1.5'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.jakewharton.timber:timber:4.1.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
Compile、testCompile、androidTestCompile依賴一樣應當分組到相對應的組別中,以下:
// App Dependencies
compile "com.android.support:support-v4:$SUPPORT_LIBRARY_VERSION"
compile "com.android.support:recyclerview-v7:$SUPPORT_LIBRARY_VERSION"
// Instrumentation test dependencies
androidTestCompile "com.android.support:support-annotations:$SUPPORT_LIBRARY_VERSION"
// Unit tests dependencies
testCompile 'org.robolectric:robolectric:3.0'
這兩種方法都可以很容易的找到特定的依賴關系,需要時,它保證依賴的聲明既干凈又整潔。
3.1.3 Independent Dependencies(獨立的依賴關系)
testCompile 'org.robolectric:robolectric:3.0'
第1時間取得博客更新提示,和更多android干貨,源碼分析,歡迎關注我的微信公眾號,掃1掃下方2維碼或長按辨認2維碼,便可關注。
如果你覺得好,隨手點贊,也是對筆者的肯定,也能夠分享此公眾號給你更多的人,原創不容易