in block variable renaming

PiNkY

Regular
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
...
}
 
What? Like putting that code in a function (possibly inlined) and calling it? ;)
 
Last edited by a moderator:
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)...
 
PiNkY said:
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)...
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.
 
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.
 
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.
 
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
 
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?
 
Back
Top