命名规则
目前,业界共有四种命名法则:驼峰命名法、匈牙利命名法、帕斯卡命名法和下划线命名法,其中前三种是较为流行的命名法。
-
驼峰命名法: 正如它的名称所表示的那样,是指混合使用大小写字母来构成变量和函数的名字。例如,下面是分别用骆驼式命名法和下划线法命名的同一个函数:
printEmployeePaychecks(); print_employee_paychecks();
第一个函数名使用了驼峰命名法,函数名中的每一个逻辑断点都有一个大写字母来标记。第二个函数名使用了下划线法,函数名中的每一个逻辑断点都有一个下划线来标记。驼峰命名法近年来越来越流行了,在许多新的函数库和Windows
这样的环境中,它使用得当相多。另一方面,下划线法是C
出现后开始流行起来的,在许多旧的程序和UNIX
这样的环境中,它的使用非常普遍。 -
匈牙利命名法:广泛应用于象
Windows
这样的环境中。Windows
编程中用到的变量(还包括宏)的命名规则为匈牙利命名法,这种命名技术是由一位能干的Microsoft
程序员查尔斯-
西蒙尼(Charles Simonyi) 提出的。匈牙利命名法通过在变量名前面加上相应的小写字母的符号标识作为前缀,标识出变量的作用域、类型等。这些符号可以多个同时使用,顺序是先m_(成员变量)、再指针、再简单数据类型、再其它。这样做的好处在于能增加程序的可读性,便于对程序的理解和维护。例如:m_lpszStr
,表示指向一个以0字符结尾的字符串的长指针成员变量。匈牙利命名法关键是:标识符的名字以一个或者多个小写字母开头作为前缀;前缀之后的是首字母大写的一个单词或多个单词组合,该单词要指明变量的用途。 -
帕斯卡(pascal)命名法:与驼峰命名法类似,二者的区别在于:驼峰命名法是首字母小写,而帕斯卡命名法是首字母大写,如:
DisplayInfo(); string UserName;
二者都是采用了帕斯卡命名法。
- 三种命名规则的小结:
MyData
就是一个帕斯卡命名法;myData
是一个驼峰命名法,它第一个单词的第一个字母小写,后面的单词首字母大写,看起来像一个骆驼;iMyData
是一个匈牙利命名法,它的小写的i
说明了它的型态,后面的和帕斯卡命名相同,指示了该变量的用途。
命名的基本原则
-
标识符的命名要清晰、明了,有明确含义,同时使用完整的单词或大家基本可以理解的缩写,避免使人产生误解——尽量采用英文单词或全部中文全拼表示,若出现英文单词和中文混合定义时,使用连字符“_”将英文与中文割开。较短的单词可通过去掉元音形成缩写;较长的单词可取单词的头几个字母形成缩写;一些单词有大家公认的缩写。例如:
temp->tmp、flag->flg、statistic->stat、increment->inc、message->msg
等缩写能够被大家基本认可。 -
命名中若使用特殊约定或缩写,则要有注释说明。应该在源文件的开始之处,对文件中所使用的缩写或约定,特别是特殊的缩写,进行必要的注释说明。
-
命名规范必须与所使用的系统风格保持一致,并在同一项目中统一。
-
对于变量命名,禁止取单个字符(如
i 、j 、k...
),建议除了要有具体含义外,还能表明其变量类型、数据类型等,但i 、j 、k
作局部循环变量是允许的。变量,尤其是局部变量,如果用单个字符表示,很容易敲错(如i
写成j
),而编译时又检查不出来,有可能为了这个小小的错误而花费大量的查错时间。
编程单词缩写规则
-
大于2个单词则采用缩写规则,否则不用缩写。
-
缩写的规则采用国际惯用方法:元音字母剔除法,首字母除外。使用单词的头一个或几个字母。
组合单词使用如下规则:
-
使用变量名中每个有典型意义的单词。如
Count of Failure
写成FailCnt
。 -
去掉无用的单词后缀
ing
、ed
等。如Paging Request
写成PagReq
。
编程单词缩写字典
序号 | 描述 | 缩写词 |
-----------------------------------------------------
A | Addition | Add |
| Accumulator | Acc |
| Address | Addr |
| Action | Act |
| Active | Act |
| Amplitude | Amp |
| Analog Input | AI |
| Anolog I/O | AIO |
| All | All |
| Alarm | Alm |
| Allocate | Alloc |
| Analog Output | AO |
| Apparent | App |
| Argument | Arg |
| Arrange | Arrng |
| Array | Array |
| Assemble | Asm |
| Attribute | Attrib |
B | Bar | Bar |
| Bit | Bit |
| Block | Blk |
| Buffer | Buf |
| Button | Btn |
| Bypass | Bypass |
C | Calibration | Cal |
| Calculate | Calc |
| Configuration | Cfg |
| Channel | Ch |
| Change | Chg |
| Check | Chk |
| Clock | Clk |
| Clear | Clr |
| Clear Screen | Cls |
| Command | Cmd |
| Compare | Cmp |
| Complete | Comp |
| Count | Cnt |
| Counter | Ctr |
| Column | Col |
| Communication | Comm |
| Connect | Con |
| Construct | Cons |
| Control | Ctrl |
| Context | Ctx |
| Convert | Conv |
| Copy | Cp |
| Current | Cur |
| Cursor | Csr |
| Control Word | CW |
D | Date | Date |
| Day | Day |
| Debounce | Debounce |
| Decrease | Dec |
| Decimal | Dec |
| Decode | Decode |
| Define | Def |
| Degree | Deg |
| Delete | Del |
| Destination | Dst |
| Descriptor | Desc |
| Device | Dev |
| Discrete Input | DI |
| Digit | Dig |
| Discrete I/O | DIO |
| Discrete Output(s) | DO |
| Disable | Dis |
| Display | Disp |
| Discovery | Disc |
| Division | Div |
| Divisor/Division | Div |
| Delay | Dly |
| Day-of-week | DOW |
| Down | Down |
| Dummy | Dummy |
| Dynamic | Dyn |
E | Edge | Edge |
| Effective | Eff |
| Electric | Elec |
| Empty | Empty |
| Enable | En |
| Engine | Eng |
| Enter | Enter |
| Entries | Entries |
| Equivalent | Equiv |
| Error(s) | Err |
| Ethernet | Eth |
| Engineering Units | EU |
| Event(s) | Event |
| Extension | Ext |
| Exit | Exit |
| Exception | Exc |
| Expiration | Exp |
| Exponent | Exp |
F | Field | Fld |
| Flag | Flag |
| Flush | Flush |
| Function(s) | Fnct |
| Format | Format |
| Fraction | Fract |
| Free | Free |
| Frequency | Freq |
| Full | Full |
G | Gain | Gain |
| Get | Get |
| Generate | Gen |
| Group(s) | Grp |
H | Handler | Handler |
| Harmonic | Harm |
| Hexadecimal | Hex |
| High | Hi |
| History | Hist |
| Hit | Hit |
| High Priority Task | HPT |
| Hour(s) | Hr |
I | I.D. | Id |
| Idle | Idle |
| Impulse | Imp |
| Input(s) | In |
| Initialization | Init |
| Initialize | Init |
| Instruction | Instr |
| Interrupt | Int |
| Invert | Inv |
| Interrupt Service Routine | ISR |
| Index | Ix |
K | Key | Key |
| Keyboard | Key |
L | Length | Len |
| Limit | Lim |
| List | List |
| Low | Lo |
| Lower | Le |
| Lowest | Lo |
| Lock | Lock |
| Low Priority Task | LTP |
M | Magnitude | Mag |
| Mantissa | Man |
| Manual | Man |
| Manufacture | Mfg |
| Maximum | Max |
| Mailbox | Mbox |
| Minimum | Min |
| Mode | Mode |
| Month | Month |
| Move | Mov |
| Message | Msg |
| Measure | Meas |
| Mask | Msk |
| Multiplication | Mul |
| Multiplex | Mux |
| Make | Mk |
N | Negative | Neg |
| Number of | Num |
| Nesting | Nesting |
| Neutral | Neut |
| New | New |
| Next | Next |
O | Offset | Offset |
| Old | Old |
| Operation System | OS |
| Optimize | Opt |
| Original | Orig |
| Output | Out |
| Overflow | Ovf |
P | Package | Pkg |
| Parameter | Param |
| Pass | Pass |
| Performance | Perf |
| Period | Per |
| Phase | Ph |
| Port | Port |
| Position | Pos |
| Positive | Pos |
| Power | Pwr |
| Previous | Prev |
| Priority | Prio |
| Printer | Prt |
| process | Proc |
| Product | Prod |
| Protocol | Prot |
| Pointer | Ptr |
| Put | Put |
Q | Queue | Q |
| Quality | Qlty |
| Quarter | Quar |
R | Raw | Raw |
| Reactive | React |
| Recall | Rcl |
| Rectangle | Rect |
| Read | Rd |
| Ready | Rdy |
| Reference | Ref |
| Register | Reg |
| Request | Req |
| Reset | Reset |
| Reserve | Resv |
| Resume | Resume |
| Response | Resp |
| Return | Rtn |
| Reverse | Revs |
| Ring | Ring |
| Row | Row |
| Repeat | Rpt |
| Real-Time | RT |
| Running | Running |
| Receive | Rx |
S | Sample | Smp |
| Scale | Scale |
| Scale Factor | SF |
| Scaling | Scaling |
| Scan | Scan |
| Schedule | Sched |
| Scheduler | Sched |
| Screen | Scr |
| Second(s) | Sec |
| Segment(s) | Seg |
| Select | Sel |
| Semaphore | Sem |
| Sequence | Seq |
| Server | Svr |
| Set | Set |
| Setting | Setting |
| Signal | Sig |
| Size | Size |
| Seven-segments | SS |
| Sourse | Src |
| Start | Start |
| Statistic(s) | Stat |
| Status | Stat |
| Stack | Stk |
| Standard | Std |
| Stop | Stop |
| String | Str |
| Subtraction | Sub |
| Suspend | Suspend |
| Switch | Sw |
| Synchronize | Synch |
| System | Syst |
T | Task | Task |
| Table | Tbl |
| Threshold | Th |
| Tick | Tick |
| Time | Time |
| Timer | Tmr |
| Toggle | Tgl |
| Total | Tot |
| Trigger | Trig |
| Time-stamp | TS |
| Timeout | TO |
U | Unlock | Unlock |
| Up | Up |
| Update | Update |
| Utility | Util |
V | Value | Val |
| Vector | Vect |
| Version | Ver |
| Variable | Var |
| Visible | Vis |
| Voltage | Vol |
W | Watchdog | Wdog |
| Write | Wr |
Y | Year | Year |
控件缩写
ADO.NET命名规范
数据类型 | 简写 | 举例 |
------------------------------------------------------------------------------
Connection | con | conNorthwind |
Command | cmd | cmdReturnProducts |
Parameter | parm | parmProductID |
DataAdapter | dad | dadProducts |
DataReader | dtr | dtrProducts |
DataSet | dst | dstNorthWind |
DataTable | dtbl | dtblProduct |
DataRow | drow | drowRow98 |
DataColumn | dcol | dcolProductID |
DataRelation | drel | drelMasterDetail |
DataView | dvw | dvwFilteredProducts |
WinForm Control命名规范
数据类型 | 简写 | 举例 |
------------------------------------------------------------------------------
Label | lbl | lblMessage |
LinkLabel | llbl | llblToday |
Button | btn | btnSave |
TextBox | txt | txtName |
MainMenu | mmnu | mmnuFile |
CheckBox | chk | chkStock |
RadioButton | rbtn | rbtnSelected |
GroupBox | gbx | gbxMain |
PictureBox | pic | picImage |
Panel | pnl | pnlBody |
DataGrid | dgrd | dgrdView |
ListBox | lst | lstProducts |
CheckedListBox | clst | clstChecked |
ComboBox | cbo | cboMenu |
ListView | lvw | lvwBrowser |
TreeView | tvw | tvwType |
TabControl | tctl | tctlSelected |
DateTimePicker | dtp | dtpStartDate |
HscrollBar | hsb | hsbImage |
VscrollBar | vsb | vsbImage |
Timer | tmr | tmrCount |
ImageList | ilst | ilstImage |
ToolBar | tlb | tlbManage |
StatusBar | stb | stbFootPrint |
OpenFileDialog | odlg | odlgFile |
SaveFileDialog | sdlg | sdlgSave |
FoldBrowserDialog | fbdlg | fgdlgBrowser |
FontDialog | fdlg | fdlgFoot |
ColorDialog | cdlg | cdlgColor |
PrintDialog | pdlg | pdlgPrint |
字母排序:
数据类型 | 简写 | |
------------------------------------------------------------------------------
AdRotator | ar | |
Button | btn | |
Calender | cal | |
CheckBox | chk | |
CheckBoxList | chklst | |
Column | col | |
ColumnHeader | ch | |
Combobox | cbo | |
CompareValidator | cv | |
CrystalReportViewer | rptvew | |
DataGrid | dg | |
DataGridView | dgv | |
DataList | dl | |
DomainUpDown | dud | |
DropDownList | ddl | |
FileUpload | ful | |
Form | frm | |
GridView | gv | |
GroupBox | grp | |
HiddenField | hf | |
Image | img | |
ImageButton | imgbtn | |
ImageList | il | |
Label | lbl | |
LinkButton | lnkbtn | |
ListBox | lst | |
ListView | lv | |
MenuStrip | ms | |
ObjectDataSource | ods | |
PagedDataSource | pds | |
Panel | pnl | |
PictureBox | pic | |
RadioButton | rdo | |
RadioButtonList | rdolst | |
RangeValidator | rv | |
RegularExpressionValidator | rev | |
Repeater | rpt | |
RequiredFieldValidator | rfv | |
StatusLabel | slbl | |
StatusStrip | ss | |
TabControl | tab | |
Table | tbl | |
TabPage | tp | |
TextBox | txt | |
Timer | tmr | |
ToolStrip | ts | |
ToolStripButton | tsbtn | |
ToolStripDropDownButton | tsddb | |
ToolStripLabel | tslbl | |
ToolStripMenuItem | tsmi | |
TreeView | tv/tvw | |