in block variable renaming

Discussion in 'PC Hardware, Software and Displays' started by PiNkY, Jun 20, 2006.

  1. PiNkY

    Regular

    Joined:
    Apr 10, 2002
    Messages:
    572
    Likes Received:
    4
    Just a short (and propably stupid) question. Does anyone know a language which supports variable renaming within blocks as a preprocessor/compiler feature for readability. At some points I'd really wish to have the abillity to do something like

    {
    ...
    <type> accumulator;
    //do something with accumulator

    ren(accumulator --> somethingElse)
    ...
    accumulator = whatever //compile time error
    ...
    }
     
  2. Simon F

    Simon F Tea maker
    Moderator Veteran

    Joined:
    Feb 8, 2002
    Messages:
    4,560
    Likes Received:
    157
    Location:
    In the Island of Sodor, where the steam trains lie
    What? Like putting that code in a function (possibly inlined) and calling it? :wink:
     
    #2 Simon F, Jun 20, 2006
    Last edited by a moderator: Jun 20, 2006
  3. PiNkY

    Regular

    Joined:
    Apr 10, 2002
    Messages:
    572
    Likes Received:
    4
    Perhaps I don't get it, but just for clarification. Imagine a large function/procedure in a language without explicit gotos. Now, what I'd wish for sometimes would be a way to rename a variable at some point within the functions scope (outside any kind of loop obviously). I'd think that'd be useful for readability (e.g. doing some complex math to derive a value in the first part of the function, that is then used in the second part of the function, so you declare it as e.g. intermediateSTH and at some point rename it valueOfSTH.) or reuse (declare int indexOfA then reuse as indexOfB)...
     
  4. Simon F

    Simon F Tea maker
    Moderator Veteran

    Joined:
    Feb 8, 2002
    Messages:
    4,560
    Likes Received:
    157
    Location:
    In the Island of Sodor, where the steam trains lie
    If you only want, say, floats or ints, then feel free to declare as many as you want. The compiler will (well should!) track the lifetimes of these variables and, in an optimised build, re-use the same phyiscal storage/registers.

    What I meant, by using functions, is that it can make things a lot easier to read and maintain if you break up a mammoth block of code into smaller pieces. Assuming we are using "C", if those subsection functions are declared "static" (and possibly inlined) the compiler can often tell if they are only used once and effectively re-assemble the larger functiona anyway.
     
  5. PiNkY

    Regular

    Joined:
    Apr 10, 2002
    Messages:
    572
    Likes Received:
    4
    First of all, thanks a lot for your fast responses. I am well aware of the benefits of breaking up larger functions into more manageable/readable chunks. What i was refering to however was not performance related at all, but just a simple preprocessor function to enhance readability for me in cases where you, e.g., have lots of accumulator type variables that "mean" different things at different points or where reuse is just obvious to avoid littering a somewhat simple function with loads of declarations.
     
  6. Rolf N

    Rolf N Recurring Membmare
    Veteran

    Joined:
    Aug 18, 2003
    Messages:
    2,494
    Likes Received:
    55
    Location:
    yes
    If you want an old name to "go away" (=> compile time error whenever you try to access it again), you can use curly-brace blocks in C. Every such block has its own variable scope.

    If you want to rename ... it has already been suggested, I'll just rephrase: declare another variable with your new desired name, assign the value from the "old" var to the "new" var and hope that your compiler optimizes it away. For extra safety you can declare the "new" var const in C. A const& may make the optimizer's job easier.

    Either/or is pretty straightforward actually. Combining both techniques is more tricky.
     
  7. PiNkY

    Regular

    Joined:
    Apr 10, 2002
    Messages:
    572
    Likes Received:
    4
    Well that's what I'm doing... Was just asking whether anyone knew a language/dialect that supports explicit renaming. Besides about the curly braces thing, I'm not well versed in c but are you suggesting anything else than the trivial

    type visibleAnywhere;
    ...
    {
    type newName = visibleAnywhere;
    ...
    }
    newName = someExpression; //compile time error
     
  8. PARANOiA

    Veteran

    Joined:
    Nov 14, 2003
    Messages:
    2,086
    Likes Received:
    9
    Location:
    Melbourne
    I would have thought renaming a variable would make your readability way, way worse. Declaring a new variable would have the same effect, though, wouldn't it?
     
Loading...

Share This Page

  • About Us

    Beyond3D has been around for over a decade and prides itself on being the best place on the web for in-depth, technically-driven discussion and analysis of 3D graphics hardware. If you love pixels and transistors, you've come to the right place!

    Beyond3D is proudly published by GPU Tools Ltd.
Loading...