Variable Names ?

J

James

Hi,
I am finding it increasingly difficult to name my variables.
I am not able to think in the right way.


Expert C programmers please Help.



Regards,
James
 
L

lallous

Hi,

It is a matter of style..

I often use this:

int iCounter, i, j, iFlag;
int nMax, nMin, nCount, n, k;
char *szName, *Buffer, p, pName, pBuffer2;
double dblX, dblY, dblZ;

hope that helps,
Elias
 
N

nitin

Hi,
I am finding it increasingly difficult to name my variables.
I am not able to think in the right way.
You can try naming them by their purpose of existance or
property they represent !

e.g.
int int1, int2;
float div;
div = int1%int2;
 
A

Allan Bruce

lallous said:
Hi,

It is a matter of style..

I often use this:

int iCounter, i, j, iFlag;
int nMax, nMin, nCount, n, k;
char *szName, *Buffer, p, pName, pBuffer2;
double dblX, dblY, dblZ;

hope that helps,
Elias

Just a small addition to this - if you are using global variables then I
prefix them with g or g_

e.g.

char gszAppName[] = "...";

Allan
 
A

Arthur J. O'Dwyer

lallous said:
It is a matter of style..

I often use this:

int iCounter, i, j, iFlag;
int nMax, nMin, nCount, n, k;
char *szName, *Buffer, p, pName, pBuffer2;
double dblX, dblY, dblZ;

Just a small addition to this - if you are using global variables then I
prefix them with g or g_

e.g.

char gszAppName[] = "...";

Wait... you forgot the following rules:

If you have more than one object with the same function, use
numerical suffixes _1, _2...

int scancnvt(int naX_1, int naY_1, int naX_2, int naY_2);

Use typedefs to clarify code:

typedef struct Spoint *pSpoint_tt; /* _t is reserved by POSIX */

Pointers involved in linked-list manipulation should be
appropriately named:

#define gpNULL NULL
pSpoint_tt gpLLroot = gpNULL;

If a linked data structure is expected to have a particular shape
for most of its lifetime, use the standard chemical nomenclature:

pSpoint_tt cyclohexagpCLLroot; /* circular linked list
with six elements */

pSpoint_tt cyclohexegpDLLroot; /* doubly linked list */

pSpoint_tt benzegproot; /* 1.5-linked list */

If two objects are used in a very similar fashion throughout a
function, then they should have very similar names - this is
the OO doctrine of "form follows function."

double rPixelAddrX, rPixelAddrY; /* measured from (cx, cy) */
int iPixelAddrX, iPixelAddrY; /* measured from (0, 0) */

When used in complex expressions like

nlRadiusy_1 = dSQR(iPixelAddrX-cx) + dSQR(rPixelAddrY);
nlRadiusy_1 /= 2;

it is not immediately obvious what the programmer's intent was.
This is called "data hiding," and is a concept central to the
study of job security.

The encoding of type information in variable names should not be
restricted only to integers and strings, however. More complex
types, such as pointers to functions, must be covered as well.

typedef int (*pIntFpaCVPCVPap_tt)(const void *, const void *);

pIntFpaCVPCVPap_tt pIntFpaCVPCVPapQSort = qsort;

Note the usage of pa...ap to represent parentheses in the type
name. This is an elegant solution to the ugliness of C's type
system, and should be used wherever possible to simplify your
code. Evangelistic tracts in the comment blocks may also help
convert the programmers who must maintain your code.

I hope these helpful hints give you something to think about.


-Arthur
TWAJS
 
B

Bertrand Mollinier Toublet

Arthur said:
-Arthur
TWAJS
I can somewhat make out the meaning of this TWAJS (especially in context
;-) However, just to make sure, I looked it up on FOLDOC and the Jargon
dictionnary and it wasn't there :-(

A google search showed that it seems to be a common danish word...

Can you just enlighten me about the meaning of the 'S', please ?
 
A

Arthur J. O'Dwyer

I can somewhat make out the meaning of this TWAJS (especially in context
;-) However, just to make sure, I looked it up on FOLDOC and the Jargon
dictionnary and it wasn't there :-(

A google search showed that it seems to be a common danish word...

Hee hee!

That was a joke, son.

-Arthur
 
D

Dan Pop

In said:
I am finding it increasingly difficult to name my variables.
I am not able to think in the right way.

Follow a set of simple conventions:

1. Variables used as auxiliaries (e.g. loop control variables, pointers
used to scan an array or to get the result of a function call,
temporary variables used to simplify longer and messier expressions
etc) should have one letter names: i, j, k for integers, p and q for
pointers, s for pointer to string, fp for pointer to FILE, x, y, z for
floating point. When you need more of them, index them with a digit:
p1, p2, x1, x2, x3... Such variables can be "recycled" in the same
function.

2. The other variables should have names suggesting their purpose. Try
to keep the names reasonably short (<= 8 characters) without
sacrificing (too much) their meaning. Don't "recycle" their names!
Use lower case letters only, so that they're easy to type.
I agree that it is not always easy to follow this rule, but,
with some practice, you seldom need to break it. However, if you feel
the need to break it, just do it, only not try to put full sentences
in a variable name.

3. Use all upper case for special identifiers, like macro names or
const "variables", to make it clear that they are special things.

4. *Sometimes*, it may make sense to label the global variables as such.
Use the g_ prefix or the _g suffix for this purpose. Likewise s_ or
_s for "static" global variables.

Dan
 
R

Randy Howard

Hungarian notation is damned awkward to start with but is very useful. My
IDE will show what the variable names are when you type a function name and
the open parenthesis, this then lets you know which type each variable is.

Actually, all it tells you is what the type was once upon a time when
first written. There is no guarantee that the type matches the name
mangling after code edits have been made (albeit incorrectly). As such
it's worthless at best, and downright dangerous the rest of the time.
 
J

James

Hi,
I think most of the discussion in this group is centered around syntax
of the c language.

I would suggest that discussions on pratical problems faced by programmers
when they try to write professional c programs.


Thanks,


James
 
R

Richard Bos

Allan Bruce said:
Hungarian notation is damned awkward to start with but is very useful.

This is debatable; I find it useless, but YMMV.
My IDE will show what the variable names are when you type a function name and
the open parenthesis, this then lets you know which type each variable is.
Nowt much, but handy all the same

This is true enough.

However, I completely fail to see the connection between those two
statements. What has a good IDE which knows how to read prototypes got
to do with Hungarian Notation?

Richard
 
R

Richard Bos

Allan Bruce said:
If the variable names are named in hungarian notation then it helps to know
what to pass to the function.

If you have a prototype, you don't need Hungarian Notation for that. If
you don't have a prototype, no amount of Hungarian Notation is going to
help you after four rounds of maintenance.

Richard
 
A

Allan Bruce

Richard Bos said:
This is debatable; I find it useless, but YMMV.


This is true enough.

However, I completely fail to see the connection between those two
statements. What has a good IDE which knows how to read prototypes got
to do with Hungarian Notation?

Richard

If the variable names are named in hungarian notation then it helps to know
what to pass to the function. I cant think of any useful examples off hand
but it has helped me in the past.
Allan
 
D

Dan Pop

In said:
I think most of the discussion in this group is centered around syntax
of the c language.

Then, you're either a patent idiot or you don't have the slightest
clue about what "syntax" means (i.e. you're an ignorant). Syntax issues
as seldom discussed in this newsgroup.
I would suggest that discussions on pratical problems faced by programmers
when they try to write professional c programs.

That's what we're doing, unless dealing with newbie issues. Except that
we don't address the pratical problems whose discussion belongs to more
specialised newsgroups.

Dan
 
J

Joona I Palaste

Then, you're either a patent idiot or you don't have the slightest
clue about what "syntax" means (i.e. you're an ignorant). Syntax issues
as seldom discussed in this newsgroup.
That's what we're doing, unless dealing with newbie issues. Except that
we don't address the pratical problems whose discussion belongs to more
specialised newsgroups.

I think James Sparker thinks that ISO standard C is only all about C
syntax, and that C semantics means platform-specific C APIs. This is
far from the truth.
If you translate Dan's reply from Danpopian to Human, you get something
like this:

C syntax question:
"Does it matter if I write char *p or char* p?"

C semantics question:
"Why is the code: char *p=malloc(strlen(s)); if (p) strcpy(p, s);
segfaulting on my computer?"

*Not* a C question at all:
"If I call WIN32_GUI_OpenDialogBox(hMainWnd, "Hello world"); the text
is not in my selected system font! How can I change the font?"

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A friend of mine is into Voodoo Acupuncture. You don't have to go into her
office. You'll just be walking down the street and... ohh, that's much better!"
- Stephen Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top