Sunday 20 November 2011

How many letters long should variable names be? What is the ANSI standard for significance? in C programming

How many letters long should variable names be? What is the ANSI standard for significance?

Generally, your variable names should be long enough to effectively describe the variable or function you are naming. Short, cryptic names should be avoided, because they often cause problems when other programmers try to interpret your code. Instead of using a short, cryptic function name such as opndatfls();
you should use a longer name such as

open_data_files();
or
OpenDataFiles();

The same is true of variable names. Instead of using a cryptic variable name such as

fmem

why not expand it to its full definition:

free_memory_available

Using expanded names will help make your code much easier to read and understand. Most ANSI compilers
allow at least 31 characters of significance—that is, only the first 31 characters are checked for uniqueness. A good rule of thumb is to make your function and variable names read just like the English language, as if you were reading a book. You should be able to read the function or variable name and easily recognize it and know generally what its function is.

Cross Reference:

XIX.1: Should the underscore be used in variable names?
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.10: What is Hungarian notation, and should I use it?

No comments:

Post a Comment