Hi,
what rules do you follow when naming variables in c programming?any
suggestion, link, etc...?
|
|
0
|
|
|
|
Reply
|
idkfaidkfaidkfa (13)
|
1/2/2010 8:50:39 AM |
|
On 2010-01-02, eryer <idkfaidkfaidkfa@gmail.com> 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 / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
|
|
0
|
|
|
|
Reply
|
Seebs
|
1/2/2010 8:53:22 AM
|
|
Seebs wrote:
<snip>
> 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.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
|
|
0
|
|
|
|
Reply
|
Richard
|
1/2/2010 9:52:39 AM
|
|
On 2 Jan, 08:53, Seebs <usenet-nos...@seebs.net> wrote:
> On 2010-01-02, eryer <idkfaidkfaid...@gmail.com> 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. =A0In
> particular, if there's only one loop index variable in sight, it's
> probably "i". =A0If there are string pointers, they're usually "s" and
> "t". =A0Conventions 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 =3D
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
|
|
0
|
|
|
|
Reply
|
Nick
|
1/2/2010 10:14:17 AM
|
|
On Jan 2, 5:14=A0pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
> =A0 =A0 receiverAutomaticGainControlSetting =3D
> 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
|
|
0
|
|
|
|
Reply
|
James
|
1/2/2010 12:07:53 PM
|
|
"eryer" <idkfaidkfaidkfa@gmail.com> wrote in message
news:15c94ffb-9e4d-4187-a527-8a1e4d76632d@j5g2000yqm.googlegroups.com...
> 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...
|
|
0
|
|
|
|
Reply
|
BGB
|
1/2/2010 1:04:45 PM
|
|
eryer wrote:
> Hi,
> what rules do you follow when naming variables in c programming?any
> suggestion, link, etc...?
I never spend more than five seconds at a time
thinking of a variable name.
If a really better name occurs to me later, then I change it.
--
pete
|
|
0
|
|
|
|
Reply
|
pete
|
1/2/2010 1:32:12 PM
|
|
eryer <idkfaidkfaidkfa@gmail.com> 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/html/CodeConventions.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.)
|
|
0
|
|
|
|
Reply
|
ram
|
1/2/2010 3:34:24 PM
|
|
On 2010-01-02, Richard Heathfield <rjh@see.sig.invalid> 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 / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
|
|
0
|
|
|
|
Reply
|
Seebs
|
1/2/2010 7:25:04 PM
|
|
On Sat, 2 Jan 2010 00:50:39 -0800 (PST), eryer
<idkfaidkfaidkfa@gmail.com> 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
|
|
0
|
|
|
|
Reply
|
Rich
|
1/2/2010 8:24:40 PM
|
|
Seebs wrote:
> On 2010-01-02, Richard Heathfield <rjh@see.sig.invalid> 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.
Count(War->Wisdom == NULL);
Extra credit if you can identify the source!
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
|
|
0
|
|
|
|
Reply
|
Richard
|
1/2/2010 8:45:26 PM
|
|
|
10 Replies
193 Views
(page loaded in 0.391 seconds)
Similiar Articles: naming convention for class attributes (member data)? - comp.lang ...When does a base member variable's name come into scope? - comp ... naming convention for class attributes (member data)? - comp.lang ... When does a base member variable ... vector splitting to many vectors - comp.soft-sys.matlabThat is, however, not possible to do as naming > that many variables on the left hand side of an assignment is not > supported by Matlab, which gives out at ... Signal-to-noise ratio for a sine signal?? - comp.soft-sys.matlab ...Signal-to-noise ratio for a sine signal?? From: kakyo ; Re: naming variables ... Re: simulink: fft calculation. From: Phil Goddard; Re: finding a Y value on a plot from a ... Large Arrays - Memory Problems - comp.soft-sys.matlab> > > > Are there any solutions that more efficiently use the matlab memory or variable naming conventions? > > What about the simple and relatively inexpensive option of ... how to use variable value as variable in the same dataset - comp ...How to pass dataset variables' value to macro - comp.soft-sys.sas ... how to use variable value as variable in the same dataset - comp ... macro- dataset naming - comp ... How to pass dataset variables' value to macro - comp.soft-sys.sas ...how to use variable value as variable in the same dataset - comp ... macro- dataset naming - comp.soft-sys.sas If there are two or more outputs from the same step you need ... ~ tilda in a function - comp.soft-sys.matlab... to use say the first one, and do not care for the second one, then you write [var,~] = my_function() This way, you do not need to worry about naming a new variable ... Pass a variable as a macro parameter. - comp.soft-sys.sas ...I thought of using a global variable and "call symput", but then realised that the global variable just gets assigned its value when the data step reaches the run ... comp.lang.python - page 2What is the naming convention for accessor of a 'private' variable? 1 47 (11/19/2009 2:27:47 AM) http://www.python.org/dev/peps/pep-0008/ The above webpage states the ... When does a base member variable's name come into scope? - comp ...naming convention for class attributes (member data)? - comp.lang ... When does a base member variable's name come into scope ... naming convention for class attributes ... Naming Variables - Software Development Tools for Windows and ...Helpful Tips Contents Naming Variables. You can name your variables almost anything you like, but there are a few rules and guidelines you should follow: Constant and Variable Naming Conventions - Microsoft Corporation ...Constant and Variable Naming Conventions. In addition to objects, constants and variables also require well-formed naming conventions. This section lists recommended ... 7/25/2012 1:55:16 PM
|