|
-
Sep 27th, 2005, 12:37 PM
#1
Thread Starter
PowerPoster
Why should Hungarian notation NOT be used?
I have been using VB for nearly 10 years (versions 3 thru 6) and have always used a form of "Hungarian" notation. My personal preferences for naming conventions are given below. Now that I have been venturing out into the .NET world, I see that Hungarian is fighting for its life, as the recommendation is NOT to use it. In that I am convinced of the value of using Hungarian to identify the type and scope of a variable, what are some valid arguments AGAINST it (other than the code looks "prettier" without it)?
-------------------------------------------------------------------------
Variable Naming Conventions (for VB6)
Many programmers follow stylistic naming conventions in an effort to improve the readability of their code. The naming conventions involve using a one-to-four character lower-case prefix to start off the name. The purpose of the prefix is to identify the data type of the variable (like "int" for integers and "str" for strings). These naming conventions are sometimes called "Hungarian naming conventions" (after Charles Simonyi, an early Microsoft programmer – and Hungarian native – who proposed the conventions). Two other prominent programmers, Leszynski and Reddick, also have led the charge for naming conventions.
The chart below shows each variable type, a recommended three-character prefix, and a sample variable name:
Code:
Variable Type Recommended Prefix Sample Variable Name
------------- ------------------ --------------------
Boolean bln blnDataIsValid
Byte byt bytSmallNum
Currency cur curBigMoney
Date dtm (for "Date/Time") dtmEmpBirthDate
Double dbl dblAnnSalary
Integer int intCounter
Long lng lngBigIntValue
Object obj objExcelApp
Single sng sngHrlyRate
String str strMyWord
Variant vnt (or "var") vntWhatever
Note the style of using a lower-case, three-character prefix, followed by a descriptive name in mixed case. This is the style of naming variables found in most VB documentation.
It is also recommended that you indicate the scope of the variable with an additional one-character prefix in front of the variable name. My recommendations are:
None for a local variable (i.e., a variable that is declared in a Sub or Function). Thus, variable names like the ones in the sample names above would be used for local variables.
The letter m for module-level variables (variables that are declared with Private scope in the General Declarations section of a code module). Such variables can be "seen" in any Sub or Function of that module. For example, a String variable declared at the module level might be named mstrEmpName.
The letter g for global (project-level) variables (variables that are declared with Public scope in the General Declarations section of a code module). Such variables can be "seen" in any Sub or Function of any module in the project. For example, a Boolean variable declared at the project level might be named gblnFirstTime.
The letter p for a variable that is passed as an argument to a Sub or Function procedure. Such a variable would have local scope within that procedure, but it is defined within the procedure's header. For example, an Integer variable passed to a Sub might be named pintCurrCode when defined in the Sub's header.
In addition, it is recommended that the letter a be used to indicate an array name. In an array name, the letter "a" would appear before the variable type indicator, but after the scope indicator. For example, an array of Boolean switches might be named as follows, depending on where it is declared:
Code:
ablnSwitches local level
mablnSwitches module level
gablnSwitches global (project) level
pablnSwitches passed parameter
Naming Conventions for Constants
Similar to naming variables, use the lowercase three-character datatype prefix ("int", "str", etc.), prefixed by an "m" if a module-level constant or "g" if a global (project-level) constant. However, for the main part of the name, use all capital letters (with underscores to break up individual words within the name).
Sample Constant Declarations:
Public Const gsngTAX_RATE As Single = 0.06
Private Const mdtmCUT_OFF_DATE As Date = #1/1/1980#
Const strERROR_MESSAGE = "Invalid Data" 'String data type assumed
"It's cold gin time again ..."
Check out my website here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|