Does this make sense?

Discussion in 'C Programming' started by Mark Healey, May 14, 2006.

  1. Mark Healey

    Mark Healey Guest

    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
     
    Mark Healey, May 14, 2006
    #1
    1. Advertising

  2. 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)
     
    Richard Heathfield, May 14, 2006
    #2
    1. Advertising

  3. Mark Healey

    Malcolm Guest

    "Mark Healey" <> 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
     
    Malcolm, May 14, 2006
    #3
  4. Mark Healey

    Simon Biber Guest

    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.
     
    Simon Biber, May 14, 2006
    #4
  5. Mark Healey

    Neil Guest

    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.
    >
    >
    >
     
    Neil, May 14, 2006
    #5
  6. 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)
     
    Richard Heathfield, May 14, 2006
    #6
  7. Mark Healey

    Mark Healey Guest

    On Sun, 14 May 2006 10:07:06 +0100, Malcolm wrote:

    > "Mark Healey" <> 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
     
    Mark Healey, May 15, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Sandler

    NULLs from SQL--does this make sense?

    Phil Sandler, Aug 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    339
    Phil Sandler
    Aug 19, 2004
  2. Jason
    Replies:
    0
    Views:
    358
    Jason
    Oct 12, 2004
  3. Timo Nentwig
    Replies:
    31
    Views:
    1,218
    Chris Smith
    May 13, 2004
  4. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    0
    Views:
    381
    milkyway
    Dec 2, 2004
  5. milkyway

    Does this make sense?

    milkyway, Dec 2, 2004, in forum: Java
    Replies:
    2
    Views:
    342
Loading...

Share This Page