Re: Blocks for scope control
Arved Sandstrom <asandstrom3minus1@eastlink.ca> writes:
Any thoughts?
Im using this whenever it makes sense. For example,
say you wanted to add a button:
{ final Button button = new Button(); frame.add( button ); }
We now have clearly documented that ?button? is just a
temporary name, used only in this line (better readability).
Moreover, when we want to add two buttons in this way, we
can copy the line
{ final Button button = new Button(); frame.add( button ); }
{ final Button button = new Button(); frame.add( button ); }
with no need to find an artificial new name for ?button?
(although one could argue against such redundant coding)
or add a loop
for( int i = 0; i < 2; ++i )
{ final Button button = new Button(); frame.add( button ); }
or apply the refactor ?extract method?:
void m( final Frame frame )
{ final Button button = new Button(); frame.add( button ); }
without ever needing to open the block and change something
inside of it (open-closed principle).