Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Naming variables

Reply
Thread Tools

Naming variables

 
 
eryer
Guest
Posts: n/a
 
      01-02-2010
Hi,
what rules do you follow when naming variables in c programming?any
suggestion, link, etc...?
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      01-02-2010
On 2010-01-02, eryer <(E-Mail Removed)> wrote:
> what rules do you follow when naming variables in c programming?any
> suggestion, link, etc...?


That's pretty open-ended.

My preference is to use naming conventions as much as possible. In
particular, if there's only one loop index variable in sight, it's
probably "i". If there are string pointers, they're usually "s" and
"t". Conventions like that go a long way towards making it easier
to read programs; big long variable names can be harder to parse.

That said, anything that isn't going to be used as often as a loop
variable typically gets a descriptive name.

I pretty much exclusively use lower_case_with_underscore; I dislike
capital letters in identifiers.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      01-02-2010
On 2 Jan, 08:53, Seebs <(E-Mail Removed)> wrote:
> On 2010-01-02, eryer <(E-Mail Removed)> wrote:



> > what rules do you follow when naming variables in c programming?any
> > suggestion, link, etc...?

>
> That's pretty open-ended.
>
> My preference is to use naming conventions as much as possible. *In
> particular, if there's only one loop index variable in sight, it's
> probably "i". *If there are string pointers, they're usually "s" and
> "t". *Conventions like that go a long way towards making it easier
> to read programs; big long variable names can be harder to parse.
>
> That said, anything that isn't going to be used as often as a loop
> variable typically gets a descriptive name.
>
> I pretty much exclusively use lower_case_with_underscore; I dislike
> capital letters in identifiers.


you can of course get pretty much the opposite advice if you ask else
where.

receiverAutomaticGainControlSetting =
AUTOMATIC_GAIN_CONTROL_ENABLED;

I've seen identifiers that weren't unique in the first 18 characters
(the compiler only checked the first eighteen characters).

I prefer very short identifiers. I'd use longer names for types. If
you have small functions then the definition of an identifier will be
only a few lines away so you know what it is.

Of course this doesn't apply to global identifiers, but you don't
have any of those do you?

I can live with either identifiers_with_underlines or
camelCaseIdentifiers
 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      01-02-2010
On Jan 2, 5:14*pm, Nick Keighley <(E-Mail Removed)>
wrote:
> * * receiverAutomaticGainControlSetting =
> AUTOMATIC_GAIN_CONTROL_ENABLED;
>
> I've seen identifiers that weren't unique in the first 18 characters
> (the compiler only checked the first eighteen characters).


This reminds me of a project I was peripherally involved with.
It was even worse than that, with header files consisting
of nothing but
#define PMI_FOO_BLOP_TWADDLE EXT_FOO_BLOP_TWADDLE
#define PMI_FOO_BLOP_TWIDDLE EXT_FOO_BLOP_TWIDDLE
...

IBM made customer an "offer they couldn't refuse" and suddenly
the need was to port this sytem from a VAX/Unix environment
to an IBM/Unix environment. Compiler using only *part* of
identifier name for uniqueness was one of the issues.

Although I was just a short-term outside contracter, the
manager thought enough of me to ask for feedback on
her bid for this porting job. $1,000,000 (One million
dollars!!) was what she proposed to ask for! I
thought of offering to do it single-handedly for a
mere half-million, but ended up just rolling my
eyeballs instead.

James
 
Reply With Quote
 
BGB / cr88192
Guest
Posts: n/a
 
      01-02-2010

"eryer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> what rules do you follow when naming variables in c programming?any
> suggestion, link, etc...?


well, different people disagree here...


for locals, I typically use short names which are letter-based:

a, b, c: often generic, sometimes doubles or array pointers;
d, e: rarely used, often floats or doubles, usually "distance" and "entropy"
or "epsilon";
f, g, h: my default names for float or double variables;
i, j, k: default names for basic integer variables;
l: may be an integer in the i, j, k set, or a float/double;
m, n: often used for bounds (min and max), but are often reversed for arrays
(num and max);
o: rarely used
p, q, r: usually void pointers;
s, t: usually string pointers;
u, v, w: usually vectors (although vectors are very often using a v0, v1, v2
convention);
x, y, z: usually floats (axis), or vectors (unit basis vectors).

'p' may often be used as a prefix for one of the above:

pi, pj, pk: integer-pointers;
pf, pg, ph: float pointers;
....

although, sometimes I instead use a, b, c as a suffix:
ia, ib, ic: integer pointers
fa, fb, fc: float pointers

I often end up using numeric suffixes if I want more vars of a given type:
i0, i1, i2, j0, j1, j2, k0, k1, k2: integer variables.

t is sometimes used as a prefix for local arrays:
char tb[256];
where I usually use numeric suffixes if I want more.

c is sometimes used for a prefix when using a pointer which is used as a
linked list:
cn: current list item
cs, ct, ce: common for when stepping over strings or buffers (ce is usually
a buffer end).

n is often used as a prefix to indicate array size, and m as a prefix for
array limits.
nia, nfa, ...

d may be a prefix for delta, such as df (delta-float) or dv0 (delta vector
0).

....

(actually, there are probably more rules than I can manage to remember all
at once...).


for most objects with complex types, I guess I usually use short (often 2
letter) nmonics for the longer type name, possibly combined with numeric
prefixes or suffixes if needed.

I very rarely use much longer names for locals, as personally I feel overly
long names makes the code unreadable (can't make sense of the code for sake
of long and unweildy expressions).


names for functions, OTOH, are very different, where I usually use:
LIBRARY_SubSys_FuncName for library internal names;
and:
pfxFuncName for public APIs;

LIBRARY_FuncName or SubSys_FuncName may often be used for private APIs
(usually, this is ones where I technically needed an API but using it is
ill-advised or needs caution).

library_funcname is sometimes used for private API utility functions.

library_subsys_varname is often used for library globals in cases where they
need to be used, however, I don't generally share globals (well, that and
DLL's don't really allow sharing globals anyways...).


or such...



 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      01-02-2010
eryer <(E-Mail Removed)> writes:
>what rules do you follow when naming variables in c programming?any
>suggestion, link, etc...?


Generally, I recommend

http://www.lysator.liu.se/c/pikestyle.html

. But it does not contain a section on variable names,
only the promise:

»More on this in the next essay.«

Maybe someone knows, where »the next essay« is?

However, it contains a very good remark about function names:

»Procedure names should reflect what they do;
function names should reflect what they return.«

Sun got this wrong, when they write instead:

»Methods should be verbs«

http://java.sun.com/docs/codeconv/ht....doc8.html#367

Of course, they do not follow their own convention in this case,
but also freely use nouns or verbal phrases as method names.

What Pike suggest can be reworded (according to me) as:

A function called mainly for its effect is an »action function«.
(It might have a result to indicate success.)

A function called mainly for its result is an »value function«.

The name of an action function should be a verbal phrase
describing the action.
(A verb here is considered to be a special case of a verbal phrase.)

The name of a value function should be a noun phrase describing
the value.
(A noun here is considered to be a special case of a noun phrase.)

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-02-2010
On 2010-01-02, Richard Heathfield <(E-Mail Removed)> wrote:
> Seebs wrote:
>> I pretty much exclusively use lower_case_with_underscore; I dislike
>> capital letters in identifiers.


> That's a clear indication that you have no standing as a computer
> scientist, and are utterly ignorant of C programming. If you had the
> slightest idea what you were talking about, you would use precisely the
> same naming conventions that I do.


You only say that because you're a gay imperialist. Also, when I was
writing limericks for Ogden Nash, he used to buy popcorn. That means
my naming convention is superior.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
Rich Webb
Guest
Posts: n/a
 
      01-02-2010
On Sat, 2 Jan 2010 00:50:39 -0800 (PST), eryer
<(E-Mail Removed)> wrote:

>Hi,
>what rules do you follow when naming variables in c programming?any
>suggestion, link, etc...?


Rule #1: If you're doing maintenance programming then use a style that's
consistent with the existing code.

Rule #2: There is no Rule #2; see Rule #1.

Rule #3: Use the naming conventions recommended by your employer,
school, clique, etc.

Otherwise, Rule #4: Choose your own preference for CamelCase or
lower_case_with_underscores. Decide how much Hungarian notation you
like. Recognize that your preferences may change over time.

Jump over to http://www.rosvall.ie/CSG/ and write your own standard!

--
Rich Webb Norfolk, VA
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
Javax.naming Exception: name not found in naming service. Harman Java 1 07-28-2006 08:51 AM
Help with Javascript: naming variables using other variables sagejoshua HTML 16 11-09-2005 12:27 PM
naming variables in code helena Java 6 01-24-2005 04:12 PM



Advertisments