变量命名和代码约定在项目中往往是一个比较难处理的议题,程序员倾向于使用其个人的命名约定,而不喜欢别人规定他们如何编写代码.
然而,当代码需要为团队内的其他成员阅读时(特别是代码检查的时候),拥有通用的命名约定是很有价值的,拥有通用的命名约定也便于自己日后再阅读自己的代码.这中体会在几次项目以后,体会更加深刻.所以才有想要统一团队命名规则的想法.
一直以来,最流行的变量命名约定是所谓的匈牙利表示法(Hungarian Notation).最初由Microsoft的Charles Simonyi提出,并且在Microsoft内部使用了许多年.这个约定规定了以标准的3或4个字母前缀来表示变量的数据类型.比如表示学生年龄的整型变量就应该命名为intStudentAge.
有了以上的想法以后,我便参照书本,列出一些变量的命名规则,具体如下:
变量命名约定
类型 |
前缀 |
例子 |
Array |
arr |
arrStudentList |
Boolean |
bln |
blnIsPostBack |
Byte |
byt |
bytPixelValue |
Char |
chr |
chrName |
DateTime |
dtm |
dtmStartTime |
Decimal |
dec |
decAverageHeight |
Double |
dbl |
dblSizeOfUniverse |
Integer |
int |
intRowCount |
Long |
lng |
lngIncome |
Object |
obj |
cbjReturnValue |
Short |
shr |
shrAverage |
Single |
sng |
sngMaximum |
String |
str |
strName |
Web控件命名
类型 |
前缀 |
例子 |
AdRotator |
adrt |
adrtTopAdv |
Button |
btn |
btnSubmit |
Calender |
cal |
calMeetingDates |
CheckBox |
chk |
chkBlue |
CheckBoxList |
chkl |
chklColorControls |
ComapreValidator |
valc |
valcValidAge |
CustomerValidator |
valx |
valxDBCheck |
DataGrid |
dgrd |
dgrdTitles |
DataList |
dlst |
dlstTitles |
DropDownList |
drop |
dropYear |
HyperLink |
lnk |
lnkDetails |
Image |
img |
imgAuntBetty |
ImageButton |
ibtn |
ibtnSubmit |
Label |
lbl |
lblErrorMess |
LinkButton |
lbtn |
lbtnSubmit |
ListBox |
lst |
lstCountries |
Panel |
pnl |
pnlForm |
RadioButton |
rad |
radFemale |
RadioButtonList |
rad |
radGender |
RangeValidator |
valg |
valgAge |
RegularExpression |
vale |
ValeEmail |
Repeator |
rpt |
rptQueryResult |
RequiredFeildVaildator |
valr |
valrName |
Table |
tbl |
tblCountryCode |
TableCell |
tblc |
tblcChina |
TableRow |
tblr |
tblrCountry |
TextBox |
txt |
txtName |
ValidationSummary |
vals |
valsFormErrors |
XML |
xmlc |
xmlcTransformResult |
PlaceHolder |
plh |
plhContents |
ADO.NET
类型 |
前缀 |
例子 |
Connection |
con |
conNorthwind |
Command |
cmd |
cmdReturnProducts |
Parameter |
parm |
parmID |
DataAdapter |
dad |
dadProducts |
DataReader |
dtr |
dtrProducts |
DataSet |
dst |
dstProducts |
DataTable |
dtbl |
dtblProduct |
DataRow |
drow |
drowRow |
DataColumn |
dcol |
dcolProductID |
DataRelation |
drel |
drelMasterDetail |
DataView |
dvw |
dvwFilterProducts | 
|