The topic of variable naming and code conventions is a touchy one. Programmers tend to develop their personal naming conventions and don't like others dictating how they should write their code.
Nevertheless, having a common convention is valuable when code must be read by multiple people in an organization (think code reviews). Having a common convention also makes it easier to read your own code if you return to it at a later date.
The most popular variable naming convention is called Hungarian Notation. It was originally developed by Charles Simonyi at Microsoft and has been used internally at Microsoft for a number of years. According to this convention, you should name all variables starting with a standard three or four letter prefix that represents the variable's data type. For example, an integer variable used to represent a customer's age should be named intCustomerAge.
Microsoft recommends that you do not follow this convention in the case of the .NET Framework and ASP.NET. The motivation for this recommendation is that they expect you to use an advanced editor, such as Microsoft Visual Studio, to write your code. Visual Studio automatically provides you with information about a variable's type.
However, in this book, I will not assume that you are using Visual Studio to write your ASP.NET pages. In practice, developers use a wide variety of editors—such as Web Matrix, TextPad, UltraEdit, and Notepad—to write code. Since I am not assuming that you are using Visual Studio, a variable naming convention is necessary.
Furthermore, in my experience, many companies require their programmers to follow a strict naming convention that is typically some variation on the Hungarian Notation. I'll be using a variation of Hungarian Notation for the naming conventions used in this book. I've created a set of prefixes and naming conventions for the basic variable data types, ASP.NET Web Controls, ADO.NET classes, and event-handling subroutines. These conventions are listed in the following tables.
My goal is not to provide a standard prefix for each and every class in the .NET Framework. That would require over 3,400 standard prefixes! The goal is to provide standard prefixes for the most common types that you would use in ASP.NET.
Type |
Prefix |
Example |
---|---|---|
Array |
arr |
arrShoppingList |
Boolean |
bln |
blnIsPostBack |
Byte |
byt |
bytPixelValue |
Char |
chr |
chrDelimiter |
DateTime |
dtm |
dtmStartDate |
Decimal |
dec |
decAverageHeight |
Double |
dbl |
dblSizeOfUniverse |
Integer |
int |
intRowCounter |
Long |
lng |
lngBillGatesIncome |
Object |
obj |
objReturnValue |
Short |
shr |
shrAverage |
Single |
sng |
sngMaximum |
String |
str |
strFirstName |
Type |
Prefix |
Example |
---|---|---|
AdRotator |
adrt |
adrtTopAd |
Button |
btn |
btnSubmit |
Calendar |
cal |
calMeetingDates |
CheckBox |
chk |
chkBlue |
CheckBoxList |
chkl |
chklFavColors |
CompareValidator |
valc |
valcValidAge |
CustomValidator |
valx |
valxDBCheck |
DataGrid |
dgrd |
dgrdTitles |
DataList |
dlst |
dlstTitles |
DropDownList |
drop |
dropCountries |
HyperLink |
lnk |
lnkDetails |
Image |
img |
imgAuntBetty |
ImageButton |
ibtn |
ibtnSubmit |
Label |
lbl |
lblResults |
LinkButton |
lbtn |
lbtnSubmit |
ListBox |
lst |
lstCountries |
Panel |
pnl |
pnlForm2 |
PlaceHolder |
plh |
plhFormContents |
RadioButton |
rad |
radFemale |
RadioButtonList |
radl |
radlGender |
RangeValidator |
valg |
valgAge |
RegularExpression |
vale |
valeEmail_Validator |
Repeater |
rpt |
rptQueryResults |
RequiredFieldValidator |
valr |
valrFirstName |
Table |
tbl |
tblCountryCodes |
TableCell |
tblc |
tblcGermany |
TableRow |
tblr |
tblrCountry |
TextBox |
txt |
txtFirstName |
ValidationSummary |
vals |
valsFormErrors |
XML |
xmlc |
xmlcTransformResults |
Type |
Prefix |
Example |
---|---|---|
Connection |
con |
conNorthwind |
Commmand |
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 |
The name of an event-handling subroutine will consist of the ID of the control that raised the event followed by the type of event being handled. For example, a subroutine named btnSubmit_Click handles the Click event of a Button control named btnSubmit.
When a control that raises an event is not assigned an ID, the type of the control is used instead of the ID. For example, the subroutine named Button_Click handles the Click event of a Button control without an ID.