/----------------------------前言--------------------------------/
作爲一個程序員,在日常的編碼過程中我們必須需要遵循一定的命名規範。可能有的人會反駁,根本不用遵循什麼規範,只要能完成功能的代碼就是好代碼。其實這種想法是很可笑的,我們在正式的開發環境下,往往不是一個人在開發,而且開發過程中可能經常需要進行重構。假如我們編碼不遵循一定的規範,而是任意妄爲,別人在 看我們代碼的時候往往花了大把的時間,還讀不懂。就算是自己獨立開發,時間久了再回來看以前的代碼也是有很大難度的,所以我們有必要去遵循這個規範。一個好的命名規範,會讓我們見名知意,很大程度上的幫助我們理解代碼。規範!規範!規範!重要的事情說三遍。
/----------------------------轉載分割線--------------------------------/
標識符命名法最常用有四種:
- Camel(駱駝)命名法:除首單詞外,其餘所有單詞的第一個字母大寫,如:fooBar;
- Pascal命名法:所有單詞的第一個字母大寫,如:FooBar;
- 下劃線命名法:單詞與單詞間用下劃線做間隔,如:foo_bar;
- 匈牙利命名法:廣泛應用於微軟編程環境中,在以Pascal命名法的變量,首字母小寫說明該變量的類型。 量的取名方式爲:scope_ prefix_qualifier 範圍前綴,類型前綴,限定詞,如:g_foo_bar;
安卓App層開發主要是Java語言,所以基本使用除了第四種外的命名方式;
縮寫在命名是必須的,遵循下面規則:
- 較短的單詞可通過去掉“元音”形成縮寫,如icon->ic;
- 較長的單詞可取單詞的頭幾個字母形成縮寫,如:average->avg;
- 此外還有一些約定成俗的英文單詞縮寫,如 Internationalization->I18N;
- 程序中不要用縮寫,除非該縮寫是約定俗成的。
命名規範:
包(packages): 採用反域名命名規則,全部使用小寫字母。一級包名爲地頂級域名如com,二級包名爲xx(可以是公司或則個人的隨便),三級包名根據應用進行命名,四級包名爲模塊名或層級名; 如 com.tinyx.myapp.activities;
類(classes):用Pascal命名法,儘量避免縮寫,如:MyActivity;縮寫是衆所周知的,如HTML,URL;類名稱中包含單詞縮寫,則單詞縮寫的每個字母均應大寫,如:PublicHTML,CommonURL。
接口(interface):與類一樣用Pascal命名法,多以able或ible結尾,多用作表示行爲,如Runnable,Accessible;
方法(methods):動詞或動名詞,採用Camel命名法,如:onCreate(),run();下面是一些建議:
- 初始化相關方法,使用init爲前綴標識,如:初始化佈局initView();
- boolean型使用is或check爲前綴標識, 如:checkValue()、isValidate();
- 返回某個值的方法,使用get爲前綴標識,如:getName();
- 數據進行處理相關,儘量使用process爲前綴標識,如:processUpdate();
- 保存數據相關,使用save爲前綴標識,如:saveData();
- 對數據重置的,使用reset前綴標識,如:resetData();
- 清除數據相關,使用clear前綴標識,如:clearData();
- 移除某些項目,使用remove前綴標識,如:removeItem();
- 繪製數據或效果相關的,使用draw前綴標識,如:drawCircle();
變量(variables):採用Pascal命名法,建議採用有意義的命名如:firstName,lastName;
01 |
public
class TestActivity extends
Activity{ |
02 |
private
ZoomableImageView mZoomableView; |
03 |
private
TabLayout mTabLayout; |
04 |
private
int mItemsCount; |
07 |
protected
void onCreate(Bundle savedInstanceState) { |
08 |
super .onCreate(savedInstanceState); |
09 |
setContentView(R.layout.test_activity); |
常量(constants): 全部大寫,採用下劃線命名法.如下:
1 |
public
static final
int MAX_ITEMS= 10 ; |
2 |
public
static final
String TAG = User. class .getSimpleName(); |
資源文件命名(resources):採用下劃線命名法,全部小寫,針對不同資源,建議用下面的命名方法;
- drawable資源,加前綴命名:前綴_功能_模塊_說明.xml/png/
說明 |
命名範例 |
---|
圖標:建議格式 ic_xxx; |
ic_appicon.png |
背景:建議格式 bg_xxx; |
bg_normal_button_default.xml,bg_normal_button_press.xml |
- layout 資源文件,前綴命名:類型_模塊_功能_說明.xml,舉一些常用的例子如下:
說明 |
命名範例 |
---|
Activity佈局文件 |
activity_main.xml |
Fragment佈局文件 |
fragment_main.xml |
局部佈局View文件 |
view_main_header.xml,view_main_bottom.xml |
自定義提示對話框 |
dialog_alert.xml |
列表項等 |
fragment_user_list_item.xml |
- 動畫anim資源文件(anim只有一種資源,所以不必加前綴區分):模塊_功能_動畫_方向.xml
說明 |
命名範例 |
---|
淡入 |
main_button_fade_in.xml |
淡出 |
main_button_fade_out.xml |
從下方推入 |
button_push_down_in.xml |
從下方推出 |
main_button_push_down_out.xml |
- menu菜單資源文件(menu只有一種資源,所以不必加前綴區分),模塊_功能_說明.xml
說明 |
命名範例 |
---|
主界面菜單 |
main_activity.xml |
Fragment界面菜單 |
user_fragment.xml |
1、 ids資源,主要存放是界面控件的id值,用下劃線小寫命名法,前綴方式:前綴_模塊_功能_說明,常用界面控件命名如下:
說明 |
命名範例 |
---|
佈局和子控件(ViewGroup,自定義View) |
view_main_topnav |
TextView |
tv_main_title |
Button |
btn_user_add |
ImageButton |
imgbtn_user_del |
ImageView |
img_thumb |
CheckBox |
cb_sex |
RadioButton |
rbtn_answer |
EditText |
et_username |
ToggleButton |
toggle_funtion |
ProgressBar |
pb_download |
SeekBar |
sb_progress |
ProgressBar |
pb_download |
VideoView |
vv_course |
WebView |
wv_download |
RantingBar |
rb_download |
Spinner |
sp_cities |
ScollView |
sv_main |
TextSwitch |
sp_cities |
ListView/ExpandListView/RecyclerView |
lv_cities |
MapView |
mv_location |
2、strings/arrays/dimens資源,用下劃線小寫命名法,不加任何前後綴,格式,模塊_功能_說明
3、attrs/colors/ids的屬性和名稱使用 Camel命名法; styles的屬性使用Camel命名法,名稱使用Pascal命名法;如下面:
02 |
< attr
name = "text"
format = "string"
/> |
03 |
< attr
name = "itemIcon"
format = "reference"
/> |
04 |
< attr
name = "showToggle"
format = "boolean"
/> |
05 |
< attr
name = "showVersion"
format = "boolean"
/> |
06 |
< style
name = "Theme.AppCompat.Light.NoActionBar" > |
07 |
< item
name = "windowActionBar" >false</ item > |
08 |
< item
name = "windowNoTitle" >true</ item > |
12 |
< color
name = "colorPrimary" >#009688</ color > |
13 |
< color
name = "colorPrimaryDark" >#00796b</ color > |
14 |
< color
name = "colorAccent" >#cddc39</ color > |
17 |
< item
name = "tabLayout"
type = "id" /> |
18 |
< item
name = "viewPager"
type = "id" /> |
19 |
< item
name = "viewContainer"
type = "id" /> |