ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
View Poll Results: Which naming convention do you prefer?
Are there any rules, conventions, gentelmen's agreements, etc. re: variable naming? I have been sticking with the my_variable method however I have been hacking someone elses php code and noticed that they seemed to switch between my_variable and MyVariable methods. Any input? Hey better yet, let do a poll. Which do you prefer? Are there any others I'm not thinking of?
I usually follow the CodingStyle document in the kernel source documentation. If you are a dork like I am reading it can be pretty funny, Linus was sure to put in a few jokes and jabs. Here is the part on naming:
Code:
Chapter 3: 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
variable "tmp", 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 it "cntusr()".
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 it "loop_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 next chapter.
I follow the Java standards these days. Lower case first word, upper case first letter on all following, for variables. Globals are all capitals. Class are all first upper case.
Originally posted by jtshaw I usually follow the CodingStyle document in the kernel source documentation.
The same, with a few variations -- therefore, we should have `var' ( or possibly `X_var'/`Xvar' where X is what the variable is related to, if `var' is already used within the scope -- though that is unlikely ). In general, as I stick with the 24-48 line max functions and splitting functions into smaller functions when they are complex ( even if you then use inlines ), it would be clear what all variables do if I named them all after alphabetic characters. The Java/Pascal/whatever style of paragraph naming is not something I do =).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.