Sunday 20 November 2011

Can a variable’s name be used to indicate its data type? in C programming

Can a variable’s name be used to indicate its data type?

Yes, indicating the data type in a variable’s name has become a very popular convention in today’s world of large, complex systems. Usually, the variable’s type is represented by one or two characters, and the variable name is prefixed with these characters. A well-known naming convention that uses this technique is called Hungarian notation, named after Microsoft programmer Charles Simonyi. Table XIX.2 contains some common prefixes
 ------------------------------------------------------------------------------------------------------------
Data Type                                     Prefix                             Example
-------------------------------------------------------------------------------------------------------------
char                                              c                                     cInChar
int                                                  i                                     iReturnValue
long                                               l                                     lNumRecs
string                                             sz                                   szInputString (terminated by zero byte)
int array                                         ai                                   aiErrorNumbers
char*                                             psz                                pszInputString
------------------------------------------------------------------------------------------------------------
Environments such as Microsoft Windows make heavy use of Hungarian notation or some derivative. Other fourth-generation environments, such as Visual Basic and Access, have also adopted a variation of the Hungarian notation.

You don’t have to stick exactly to a particular notation when writing your programs—it is perfectly OK to create your own customized derivative. This is especially true when you are creating notations for your own typedefs. For instance, if you have a typedef named SOURCEFILE that keeps information such as the source filename, handle, number of lines, last compile date and time, number of errors, and so on, you might want to create a prefix notation such as “sf ” (source file). This way, when you see a variable named sfBuffer, youknow that it refers to a variable that holds the contents of your SOURCEFILE structure.

Whatever the case may be, it is a good idea to adopt some form of naming convention for your variables and functions. This is especially true when you are working on large projects with many different programmers or when you are working in environments such as Microsoft Windows. Adopting a well-thought-out naming convention might help you make your programs more readable, especially if your code is extremely complex.

Cross Reference:

XIX.1: Should the underscore be used in variable names?
XIX.5: What is camel notation?
XIX.6: Do longer variable names affect the speed, executable size, or efficiency of a program?
XIX.9: How many letters long should variable names be? What is the ANSI standard for significance?
XIX.10: What is Hungarian notation, and should I use it?

No comments:

Post a Comment