Sunday 20 November 2011

Should the underscore be used in variable names? in C programming

Should the underscore be used in variable names?

Using the underscore in variable names is a matter of style. There is nothing wrong with using underscores or avoiding them altogether. The important thing to remember is to be consistent—use the same naming conventions throughout your application. This means that if you are programming in a team environment, you and your team members should decide on a naming convention and stick with it. If not everyone uses the same convention, your program integration will be horrible and hard to read. Additionally, you should adhere to the style used by the third-party libraries (if any) that are used in your program. If at all possible, use the same naming convention as the third-party library. Doing so will make your programs more readableand consistent.

Many C programmers find the underscore method of variable naming to be convenient. Perhaps this is because the underscore method tends to be very readable. For instance, the following two function names are similar, but one could argue that the underscored function name is more readable:

check_disk_space_available(selected_disk_drive);

CheckDiskSpaceAvailable(SelectedDiskDrive);

The second notation used here is called camel notation—see FAQ XIX.5 for an explanation of camel
notation.

Cross Reference:

XIX.2: Can a variable’s name be used to indicate its data type?
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