473,513 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this make sense?

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.

--
Mark Healey
marknews(at)healeyonline(dot)com

May 14 '06 #1
6 1303
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
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 14 '06 #2
Mark Healey wrote:
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 */
}
}
}

--
Simon.
May 14 '06 #3
Mark Healey wrote:
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.

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.

May 14 '06 #4
"Mark Healey" <di*@spammer.die> wrote in message
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.
--
www.personal.leeds.ac.uk/~bgy1mm


May 14 '06 #5
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 14 '06 #6
On Sun, 14 May 2006 10:07:06 +0100, Malcolm wrote:
"Mark Healey" <di*@spammer.die> wrote in message
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.


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

--
Mark Healey
marknews(at)healeyonline(dot)com

May 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4600
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
3
29661
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
0
417
by: mike | last post by:
regards: Does the following programming architecture make sense? http://www.wretch.cc/album/show.php?i=otp&b=1&f=1111993473&p=2 ...
10
5738
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address,...
1
2142
by: mikeotp | last post by:
Does the statement make sense? Meaning of “program” is to setup *.h(header檔) and to edit a main file to be a program exit. The main file includes all of the“*.h” files I edited before....
3
1276
by: **Developer** | last post by:
I have a usercontrol that contains two pictureboxes One is on top of the other. The bottom one is the parent of the top one. The top one is transparent. I invalidate the top one often
3
2178
by: electrician | last post by:
Yes, no GOTO. This is a major blunder on part of the creators of these tools. GOTO gives the programmer the absolute control over the program. Yes, no matter what, a GOTO sends the program to...
33
2297
by: dragoncoder | last post by:
Hi all, Does the following code invoke undefined behaviour ? $ cat a1.cc #include <iostream> #include <limits> int main() { int a = INT_MAX/2;
48
4198
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
2
1251
by: puzzlecracker | last post by:
I have never seen this in practice and interested in its pros, or ever existential (another words, standardized) possibility? Thanks, Sasha
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.