LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


View Poll Results: Which naming convention do you prefer?
MyVariable 0 0%
my_variable 5 100.00%
Voters: 5. You may not vote on this poll

Reply
  Search this Thread
Old 02-25-2004, 09:02 AM   #1
patpawlowski
Member
 
Registered: Mar 2003
Location: Centreville, Virginia
Distribution: Mandrak, Red Hat
Posts: 163

Rep: Reputation: 30
Question 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?
 
Old 02-25-2004, 09:54 AM   #2
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
i like to keep everything lower case if i can help it. but instead of my_variable i'd just do myvariable.
 
Old 02-25-2004, 10:19 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 02-25-2004, 10:47 AM   #4
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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.
 
Old 02-25-2004, 11:17 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I use the "word1_word2_word3" form
 
Old 02-25-2004, 11:19 AM   #6
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
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 =).
 
Old 02-25-2004, 01:49 PM   #7
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Naming Conventions logosys Programming 0 11-10-2005 08:21 PM
Supermount naming conventions --- how to edit??? esteeven Mandriva 0 08-31-2004 02:43 PM
Samba file naming conventions ElectroLinux Linux - Newbie 3 03-10-2004 02:42 AM
Naming conventions ??? bigjohn Linux - Newbie 1 12-07-2003 10:25 AM
File Naming Conventions mooreted Linux - Software 4 08-23-2003 10:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration