Create Loosely Coupled, Highly Specialized Procedures

Hungarian Notation

The first evolution of naming conventions was the use of single character prefixes (for example, Dim lWidth As Long and Dim iHeight As Integer ). The single-character prefix became popular, but it was doomed from the beginning. There are only 26 unique characters (a z) available for single-character prefixes, but there are many different types of variables and objects, and many of them share the same first letter.

A better naming convention eventually replaced the single-character prefix. This convention using a three-character prefix to denote data types and control types is known as Hungarian notation, partly because its creator, Charles Simonyi, was originally from Hungary, and partly because these prefixes tend to make variable names look like a foreign language. Prefixes can grow in length as additional information is denoted, such as scope or the fact that a variable is an array. Although the standard prefixes are usually three characters, with the possible addition of modifiers, conventions using larger prefixes are also referred to as Hungarian notation Hungarian notation is a concept, not a specific implementation.

Note

Ever since their inception, naming conventions have been controversial. While some developers swear by the use of a naming convention, others are just as vehemently against the idea. Who's right? Well, neither side, actually. As with many things, it's a matter of taste. If you're comfortable with using Hungarian notation, or you don't have a strong opinion either way, then I strongly encourage you to do so. If you're still against using Hungarian notation after reading this chapter, then I ask that you try to ignore the fact that I use it throughout this book.


 

In Hungarian notation, a unique three-character prefix is assigned to each data type and to each type of control. (Some prefixes may extend beyond three characters.) The three characters allow for plenty of versatility and for prefixes that are logical and intuitive. Look at the following variable declarations and you'll see how the prefixes align fairly intuitively with the variables' assigned data types:

Dim strName      As String Dim intAge       As Integer Dim lngMiles     As Long Dim dblSalary    As Double 

The primary use of Hungarian notation is to make it easy for a reader to know exactly what type of data is being used in code.

Here's the statement shown earlier, only this one uses Hungarian notation. Is there any doubt about the data types and control types that are referenced in this statement?

txtTotalDue.Text = CStr(CDbl(lblLineItemTotal.Text) - _                    CDbl(sngDiscount) + intTotalTax) 

As you can see, prefixes make the statements much more understandable. Simply reading the statement tells you everything that is happening; you don't have to look at variable declarations to determine what's going on.

Hungarian notation provides other benefits in addition to creating self-documenting code. For example, controls are listed by control type and then by name in the drop-down list of the Properties window. This makes it easy to scan the list for a specific control when you're unsure of the name but you do know the type. The prefix can also be useful in code when you need to work with controls of a specific type.

Another benefit of using Hungarian notation is that you can have similarly named variables or objects. For example, if you have a text box used to enter a first name, you're probably going to have a corresponding label control. Using Hungarian notation, you could name the text box txtFirstName and the label control lblFirstName. Without Hungarian notation, you'd have to find some other common naming scheme to denote such a relationship. This same principle holds true for variables, where you might have two variables that are similar but of differing types; prefixes once again allow you to use the most appropriate name without fear of creating a conflict.



Practical Standards for Microsoft Visual Basic. NET
Practical Standards for Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 0735613567
EAN: 2147483647
Year: 2005
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net