LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   variable naming conventions (https://www.linuxquestions.org/questions/programming-9/variable-naming-conventions-150327/)

patpawlowski 02-25-2004 09:02 AM

variable naming conventions
 
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?

chewysplace 02-25-2004 09:54 AM

i like to keep everything lower case if i can help it. but instead of my_variable i'd just do myvariable.

jtshaw 02-25-2004 10:19 AM

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.


coolman0stress 02-25-2004 10:47 AM

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.

itsme86 02-25-2004 11:17 AM

I use the "word1_word2_word3" form :)

cjcuk 02-25-2004 11:19 AM

Quote:

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 =).

Strike 02-25-2004 01:49 PM

I follow the coding standard of whatever project I am working on. As should everyone else.

Personally I am equally comfortable with either and often find myself writing one way for one program and another way for another one.


All times are GMT -5. The time now is 10:36 AM.