Hi,
Often when I introduce a new variable declaration in the middle of a block
it is just for temporary purposes, so I would like that variable to go out
of scope when I'm done with it, rather than staying in scope until the end
of the function.
A trick I have occasionally used is to include an artificial inner block
e.g.
....
{
int workingValue=0;
...
}
// now I know workingValue has gone out of scope
do you folks think this is a viable programming practice, or does the
apparently superfluous level of nesting just serve to hamper readability and
confuse people?
Andy
|
|
0
|
|
|
|
Reply
|
ajfish (393)
|
8/15/2003 3:07:05 PM |
|
"Andy Fish" <ajfish@blueyonder.co.uk> wrote:
> Hi,
>
> Often when I introduce a new variable declaration in the middle of a block
> it is just for temporary purposes, so I would like that variable to go out
> of scope when I'm done with it, rather than staying in scope until the end
> of the function.
>
> A trick I have occasionally used is to include an artificial inner block
> e.g.
>
> ...
> {
> int workingValue=0;
> ...
> }
> // now I know workingValue has gone out of scope
>
> do you folks think this is a viable programming practice, or does the
> apparently superfluous level of nesting just serve to hamper readability
and
> confuse people?
>
Local variables, regardless of block scope, are all allocated on the stack
at the beginning of the method call.
But, if you use multiple block-scoped local variables, then stack slots
might be re-used.
For example:
int a;
char b;
allocates two stack slots. So does:
int a;
// two stack slots exist; one for a, and one for b, coming up.
{
char b;
// do something else
}
// b's stack slot is still allocated, and will remain so until
end-of-method.
----------------------------------------------
But
{
int a;
}
{
char b;
}
The variables a and b might be able to share a stack slot.
My take on things: don't go cluttering your code with inner blocks just
because local variables go out of scope that much sooner. Unless you first
prove that you have a problem, and that changing your source in this manner
improves your situation. Given the discussion above, using many block-scoped
local variables is unlikely to help unless: 1) You have a method that
requires many, many short-lived variables that is 2) highly recursive. (This
is where a small stack frame might help.)
-- Adam Maass
|
|
0
|
|
|
|
Reply
|
adam.nospam.maass (228)
|
8/16/2003 12:28:18 AM
|
|
"Andy Fish" <ajfish@blueyonder.co.uk> wrote in message news:<tG6%a.2461$1Y5.16339107@news-text.cableinet.net>...
> Hi,
>
> Often when I introduce a new variable declaration in the middle of a block
> it is just for temporary purposes, so I would like that variable to go out
> of scope when I'm done with it, rather than staying in scope until the end
> of the function.
>
> A trick I have occasionally used is to include an artificial inner block
> e.g.
>
> ...
> {
> int workingValue=0;
> ...
> }
> // now I know workingValue has gone out of scope
>
> do you folks think this is a viable programming practice, or does the
> apparently superfluous level of nesting just serve to hamper readability and
> confuse people?
>
> Andy
This is fine. I generally place a //-style comment at the
start of the block, and the same comment at the ending brace
to aid readability (depending on the size of the block).
I generally do not like the usual approach of sprinkling
local variables throughout the same-level block. I prefer
to have them all at the start of the block. However, there
are times when some of the local variables are just too
short-lived to merit such placement. In that case, I simply
open a new block, declare and use the variable, then shut
the block.
Do not worry about such details as reusing stackframe slots,
since that is compiler-dependent. Go for readability and
maintainability. You (and others) will spend far more time
reading the code than writing it.
|
|
0
|
|
|
|
Reply
|
xarax (448)
|
8/16/2003 1:24:04 PM
|
|
Andy Fish wrote:
> A trick I have occasionally used is to include an artificial inner
> block e.g.
>
> ...
> {
> int workingValue=0;
> ...
> }
> // now I know workingValue has gone out of scope
>
My immediate thought is that if there's enough stuff after workingValue has
gone out of scope for the "trick" to have any value, then I wouldn't
particularly want to work with that code whether the trick is used or not.
-- chris
|
|
0
|
|
|
|
Reply
|
chris.uppal (3970)
|
8/16/2003 2:51:51 PM
|
|
|
3 Replies
23 Views
(page loaded in 0.088 seconds)
Similiar Articles: Explanation needed for const int "error: variably modified ... at ...For example: >> Note that this is legal (at block scope): >> >> const int r ... Automatic const variables that are defined without an initialiser are not even ... get static variable from class into JSP tag scope - comp.lang.java ...Hi All,I have a static variable in a class that I wish to share with some ofmy JSP code. Can I use the (or any tag?) to get that mapinto the scope of... Blocking and non blocking assignment in VHDL - comp.lang.vhdl ...... the statements in the always block ... Variables are like regs. Variables (mostly) can only be declared within processes, and have a scope that is restricted to that ... local variables - comp.text.tex... the free encyclopedia In computer science, a local variable is a variable that is given local scope. Such a variable is accessible only from the function or block in which ... extern typedef struct - comp.lang.c... obj = new_object(); /* definition needs to be at file scope, not block scope, if it ... If 'obj' is meant to be a global variable it can be defined in any of the source ... SIMULINK Counter - comp.soft-sys.matlabHi, I need a counter in an Embedded Matlab block in ... how to add two or more signal to scope in simulink ... comp.soft ..... of this counter through some variable from ... Plotting a graph in simulink - comp.soft-sys.matlabThe channel model is fading with AWGN block (with a ... command >>sim Set the value of Eb/N0 as a variable in ... can export the graph/plot I get on the Vector Scope block ... External variables - comp.lang.asm.x86An array is a contiguous block of memory that contains the data itself ... ODE -- External Variables External variables are declared in the <variables> section of the <scope ... struct/union difference - comp.lang.c++.moderatedC++ treats the members of an unnamed, untagged local union as variables of the scope which ... that is difficult to say any other way (well we could handle a block of raw ... how to overcome "java.lang.OutOfMemory" Error - comp.lang.java ...The solution is to use "temporary" variables that go out of scope (the block ends) as the only references to such objects. Avoid static and collection references to ... Scope (computer science) - Wikipedia, the free encyclopediaSince the main use of blocks within a function is in control structures such as if-statements and while-loops, block scope tends to link the scope of variables to the ... Scope (C++) - Microsoft Corporation: Software, Smartphones, Online ...... class constructors and destructors are called, and when variables local to the scope ... The names of formal arguments to a function in the scope of the outermost block of ... 7/28/2012 10:52:10 AM
|