Does this make sense?

M

Mark Healey

I have a program that runs an array through several loops in a single
function. For readability I'd like to use descriptive variable names in
each loop. I could just declare them at the beginning of the function but
it bugs me that I'm using more variables in the function than I need just
for readability purposes.

Is there something analogous to Linux hard links where I could have a line
something like:

&var1=&var1;

so I'm only using the space for one instance of the variable but in the
source code it can be addressed by different names without resorting to
pointer operations (where I usually create hard to find bugs) beyond
declaration of the variables.
 
R

Richard Heathfield

Mark Healey said:
I have a program that runs an array through several loops in a single
function. For readability I'd like to use descriptive variable names in
each loop.

Loop counters? If it's the same array each time, just call it ThisElement
(or some improvement on that).
I could just declare them at the beginning of the function but
it bugs me that I'm using more variables in the function than I need just
for readability purposes.

You can destroy them when you're finished with them:

void proc(unsigned char *p, size_t n)
{
{
size_t loopcounter;
for(loopcounter = 0; loopcounter < n; loopcounter++)
{
usethevalueof(p[loopcounter]);
}
}
{
size_t newloopcounter;
for(newloopcounter = 0; newloopcounter < n; newloopcounter++)
{
usethevalueof(p[newloopcounter]);
}
}
}
Is there something analogous to Linux hard links where I could have a line
something like:

&var1=&var1;

so I'm only using the space for one instance of the variable but in the
source code it can be addressed by different names without resorting to
pointer operations (where I usually create hard to find bugs) beyond
declaration of the variables.

You mean something like this?

void proc(unsigned char *p, size_t n)
{
size_t x;
#define loopcounter x
for(loopcounter = 0; loopcounter < n; loopcounter++)
{
usethevalueof(p[loopcounter]);
}
#undef loopcounter
#define newloopcounter x
for(newloopcounter = 0; newloopcounter < n; newloopcounter++)
{
usethevalueof(p[newloopcounter]);
}
#undef newloopcounter
}
 
M

Malcolm

Mark Healey said:
I have a program that runs an array through several loops in a single
function. For readability I'd like to use descriptive variable names in
each loop. I could just declare them at the beginning of the function but
it bugs me that I'm using more variables in the function than I need just
for readability purposes.
Just for readability?
Normally readability is second in importance only to correctness. Rarely you
need to sacrifice readability for efficiency.

If the code is easier to follow with a separate variable name for each loop
counter, then just declare the variables. It is highly unlikely to matter
that you are putting a few extra integers on the stack.
 
S

Simon Biber

Mark said:
I have a program that runs an array through several loops in a single
function. For readability I'd like to use descriptive variable names in
each loop. I could just declare them at the beginning of the function but
it bugs me that I'm using more variables in the function than I need just
for readability purposes.

Is there something analogous to Linux hard links where I could have a line
something like:

&var1=&var1;

so I'm only using the space for one instance of the variable but in the
source code it can be addressed by different names without resorting to
pointer operations (where I usually create hard to find bugs) beyond
declaration of the variables.

I think what you're describing is similar to C++'s reference types. The
following example shows how I create a descriptive short-hand notation
to refer to a particular cell in an array.

for(int y = 0; y < 9; y++)
{
for(int x = 0; x < 9; x++)
{
Cell& cell = cells[y * 9 + x];

if(cell.contains(val))
{
/* do something with cell */
}
}
}

There's no exact equivalent in C, but you can use pointers:

for(int y = 0; y < 9; y++)
{
for(int x = 0; x < 9; x++)
{
Cell *cell = &cells[y * 9 + x];

if(cell_contains(cell, val))
{
/* do something with cell */
}
}
}
 
N

Neil

Mark said:
I have a program that runs an array through several loops in a single
function. For readability I'd like to use descriptive variable names in
each loop. I could just declare them at the beginning of the function but
it bugs me that I'm using more variables in the function than I need just
for readability purposes.

That is the compilers job. You are assuming the compiler will not reuse
the space. Many will use the memory location of a variable if it is
not used anymore in that function.
 
R

Richard Heathfield

Malcolm said:
Normally readability is second in importance only to correctness.

Arguably, readability is even more important than correctness, because if
you can't read it you can't be sure it's right and you can't fix it if it's
wrong.
 
M

Mark Healey

Just for readability?
Normally readability is second in importance only to correctness. Rarely
you need to sacrifice readability for efficiency.

If the code is easier to follow with a separate variable name for each
loop counter, then just declare the variables. It is highly unlikely to
matter that you are putting a few extra integers on the stack.

That's definitely true in this case but it still bugs me.
 

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
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top