TIL how Linux C coding style argues for using short local variable names
TIL how Linux C coding style argues for using short local variable names: Linux C coding Style: Naming
C is a Spartan language, and so should your naming be. Unlike Modula-2 and Pascal programmers, C programmers do not use cute names like
ThisVariableIsATemporaryCounter
. A C programmer would call that variabletmp
, which is much easier to write, and not the least more difficult to understand.HOWEVER, while mixed-case names are frowned upon, descriptive names for global variables are a must. To call a global function foo is a shooting offense.
GLOBAL variables (to be used only if you really need them) need to have descriptive names, as do global functions. If you have a function that counts the number of active users, you should call that
count_active_users()
or similar, you should not call itcntusr()
.Encoding the type of a function into the name (so-called Hungarian notation) is brain damaged - the compiler knows the types anyway and can check those, and it only confuses the programmer. No wonder MicroSoft makes buggy programs.
LOCAL variable names should be short, and to the point. If you have some random integer loop counter, it should probably be called
i
. Calling itloop_counter
is non-productive, if there is no chance of it being mis-understood. Similarly,tmp
can be just about any type of variable that is used to hold a temporary value.If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome. See chapter 6 (Functions).