LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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


Reply
  Search this Thread
Old 02-13-2010, 07:58 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - Function naming convention


What's most commonly used and what do you prefer?

C-Style (this_is_my_function), Camelcase 1 (thisIsMyFunction), Camelcase 2 (ThisIsMyFunction), or other (please explain if other)?

Thanks!
 
Old 02-13-2010, 08:05 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I like to use lowerCamelCase for functions and vars, and CamelCase for classes/data types.
 
Old 02-14-2010, 12:07 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, Golmschenk -

Here's a great "readme" written by Linus Torvalds himself (you know - the guy who created Linux):

Quote:
http://kerneltrap.org/files/Jeremy/CodingStyle.txt

Coding style is very personal, and I won't _force_ my
views on anybody, but this is what goes for anything that I have to be
able to maintain, and I'd prefer it for most other things too. Please
at least consider the points made here.
...
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.
...
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.
...
 
Old 02-15-2010, 10:38 AM   #4
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Cool cool. Thanks to both of you for your input.
 
Old 02-15-2010, 08:08 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
off topic: how to name variables

Quoth Mr. Torvalds:
Quote:
If you have some random integer loop counter, it should probably be called "i".
No. No no no. No.

If you call something "i", and you use your text editor to find all occurrences of that variable, you'll also stop at every variable whose name inclues "i", and every "if", and who knows how many places in your comments.

I like to follow the rule that no variable's name should be found as part of any other name. It makes searching for all occurrences of a variable far, far easier.
 
Old 02-15-2010, 10:57 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Nonsense! "i" is a *perfect* name for a temporary index ... for a loop ... that's only going to span a couple of lines anyway.

If your loop is out-of-control to the point you *need* a text editor to find the index ... then you've got bigger problems than naming conventions for variables.

IMHO .. PSM
 
Old 02-16-2010, 12:46 AM   #7
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
I prefer ThisIsAStruct, THIS_IS_A_CONSTANT, this_is_a_variable, and ThisIs_aPseudoMemberFunctionOf(ThisIs*).

At work, CThisIsAClass, ThisIsAFunction, THIS_IS_A_STRUCT_s, THIS_IS_AN_ENUM_e, conjAnd pnThese vAre nVariables, because our code used to be Windows-only and that's what MFC does.
 
Old 02-16-2010, 05:32 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by paulsm4 View Post
Nonsense! "i" is a *perfect* name for a temporary index ... for a loop ... that's only going to span a couple of lines anyway.
I don't agree, but you have an interesting point. I believe that this question is a minor example of this.
 
Old 02-16-2010, 09:00 AM   #9
mjones490
Member
 
Registered: Sep 2005
Distribution: LFS
Posts: 60

Rep: Reputation: 22
Quote:
Originally Posted by wje_lq View Post
Quoth Mr. Torvalds:

No. No no no. No.

If you call something "i", and you use your text editor to find all occurrences of that variable, you'll also stop at every variable whose name inclues "i", and every "if", and who knows how many places in your comments.

I like to follow the rule that no variable's name should be found as part of any other name. It makes searching for all occurrences of a variable far, far easier.
I tend to follow the rule "When in Rome, do as the Romans do." If I'm working in a project started by someone else or as part of a team, I'll follow their naming conventions (and brace placement, and tabs vs. spaces, etc.). But on this I have to chime in.

This is a very weak excuse at best for not using "i" as a loop variable name. If you're searching occurrences of variable names, typically you're searching for global variables (or member variables, in C++) which should have longer, more descriptive names anyway. If you want to find the loops themselves, look for "for" or "while". If you want to find every occurrence of "i" within that loop code, you should be able to eyeball it. If you can't eyeball it, there is too much code in the loop and it needs to be broken up or re-written.
 
Old 02-16-2010, 07:21 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, mjones490 -
Quote:
I tend to follow the rule "When in Rome, do as the Romans do." If I'm working in a project started by someone else or as part of a team, I'll follow their naming conventions (and brace placement, and tabs vs. spaces, etc.).
I agree completely!

Let's face it: the whole POINT of "coding conventions" is to instill "consistency". A code base that's "consistent" is easier to read; a code base that's easier to read helps the team and helps the project. An all around good thing.

If I've got a "better idea" and start violating those conventions (position my braces differently, name my variables differently - whatever), then I'm injecting INCONSISTENCIES. Regardless of how one feels about names or braces ... doing it DIFFERENTLY ... is worse than no coding conventions at all.

IMHO .. PSM
 
Old 02-16-2010, 08:14 PM   #11
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by paulsm4 View Post
Let's face it: the whole POINT of "coding conventions" is to instill "consistency". A code base that's "consistent" is easier to read; a code base that's easier to read helps the team and helps the project. An all around good thing.

If I've got a "better idea" and start violating those conventions (position my braces differently, name my variables differently - whatever), then I'm injecting INCONSISTENCIES. Regardless of how one feels about names or braces ... doing it DIFFERENTLY ... is worse than no coding conventions at all.
In general, this is a good idea, and I guess in principle I'll have to agree with it. (You'll note that my dissent in this thread had to do with an entirely different issue.)

I would just point out that there have been times where I've looked at some modification to some C code and asked myself, "What's going on here?" And then I looked at the braces, the commenting style, and so forth, and thought, "Wait, that's Bryan's handwriting, I'll go ask him."

This might make you gag, and it makes me gag, too, but it's helped from time to time.
 
  


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
LXer: New NVIDIA Naming Convention LXer Syndicated Linux News 0 02-25-2007 12:16 PM
file naming convention soldan Linux - Newbie 13 05-15-2006 11:11 AM
usb naming convention hoover93 Linux - Hardware 5 10-19-2005 10:18 PM
mail server the naming naming convention problem kashan Linux - Newbie 0 07-16-2004 02:08 PM
A small question in naming convention mnguyen Linux - Software 1 04-17-2004 03:14 AM

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

All times are GMT -5. The time now is 04:28 PM.

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