转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52614696
前言:Android中一些开发规范,避免给自己和别人少留坑。
二、代码相关2.2.15 Field Ordering 属性排序 在类文件顶部声明的任何属性都应该按下列的排序规则进行排序:
- 1.Enums (枚举类型)
- 2.Constants (常量)
- 3.Dagger Injected fields (Dagger注入的属性)
- 4.Butterknife View Bindings (Butterknife绑定的view)
- 5.private global variables (private成员变量)
- 6.public global variables (public成员变量) 例如:
//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.Constants
- 2.Fields
- 3.Constructors
- 4.Override methods and callbacks (public or private)
- 5.Public methods
- 6.Private methods
- 7.Inner classes or interfaces
例如:
//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上下文参数应放在第一位,并且Callback回调参数放置在最后
2.2.18 String constants, naming, and values 字符串常量、命名和值 当使用字符串常量时,其应该修饰为静态final并且遵循下列规则:
2.2.19 Enums 枚举 枚举的使用仅仅在实际需要用到时。如果另外一种方法可行,此时应该选择更好的方式去实现它,例如: 相对于下面这样:
//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传递数据时,值的键必须使用下面定义的约定:
-
Activity
- 传递数据到一个activity必须使用一个KEY的引用,像下面这样定义: private static final String KEY_NAME = “com.package.name.activity.KEY_NAME”;
-
Fragment
- 传递数据到一个fragment必须使用一个EXTRA的引用,像下面这样定义: private static final String EXTRA_NAME = “EXTRA_NAME”;
当创建fragment或者activity的新实例涉及到传递数据时,我们应该提供一个静态的方法来获取新的实例,传递的数据作为参数。例如:
//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个字符,这样代码的可读性会更高。有时为了实现上述要求,我们需要做的:
- 1.提取数据到一个局部变量
- 2.提取代码逻辑到外部的方法
- 3.将一段较长的代码换行显示
- 注意:对于代码的注释和导入声明,超过100个字符的限制是可以的。
2.2.21.1 Line-wrapping techniques 换行技巧 当涉及到换行时,有一些情况我们应该保持与格式化代码的一致性。
- 运算符换行 当我们需要在一个运算公式换行时,需要在运算符前面换行:
//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;
- 方法链 当涉及到方法链时(这个比较流行,建议写方法链,RxJava几乎都是),每个方法的调用都应该另起一行: 不要下面这样:
//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(方法间间距) 在同一个类中,方法与方法之间只需要留有一行的空白,如下:
//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 { }
- 2.2.23.3 Class comments(类注释) 在创建类注释时,它们应该是有意义的,有描述性的,必要的时候使用超链接。如下:
/** * 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">Savestring> <string name="button_cancel">Cancelstring> // Settings Activity <string name="message_instructions">...string>
这样写不仅可以让string文件看起来整洁,还能在需要更改它们时更容易找到。
2.2.24.3 RxJava chaining(RxJava链接) 当进行异步操作时,每一步操作都应该在遇到“.”号之前另起一行,如下:
//create by 逆流的鱼yuiop on 2016/9/22 //blog地址:http://blog.csdn.net/hejjunlin return dataManager.getPost() .concatMap(new Func1关注打赏