Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Blocks for scope control Date: Sun, 15 Jan 2012 17:10:53 +0100 Lines: 37 Message-ID: <9ngc4oFl2gU1@mid.individual.net> References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net mCfx6CVJ4uEa9oDAFvRl4QblqpXCQxi/8NDjjPb1GODQJMItQ= Cancel-Lock: sha1:ww+zUAEYej3PCp6Je+jf1xoYENw= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 In-Reply-To: Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11345 On 15.01.2012 16:50, Arved Sandstrom wrote: > See > http://stackoverflow.com/questions/1563030/anonymous-code-blocks-in-java > for a general discussion. > > I'm talking specifically about the block by itself: a pair of braces by > themselves, enclosing some code in a method. On occasion I do run across > situations where a judicious use of such a block inside a method, to > restrict variable visibility, is handy. > > I almost never see anyone else using these things this way. A block in a > method always seems to be associated with a control construct or > exception handling. As any number of references point out, you wouldn't > expect to see many usages of local blocks, mainly because 99 times out > of 100 that situation is probably handled best by a new method. But in > my years of looking at Java it occurs to me that I barely see any use of > this at all...even where it wouldn't be a bad idea. > > Any thoughts? I can't remember ever having used a block without flow control inside a method for other purposes than experimenting with scoping rules. And I am not missing anything. I believe the fact that variables can be declared in a for loop header as well as that all loops and if-else open a new scope (as well as methods, of course) is responsible for me never using blocks inside methods. IMHO chances are that if you need one or more blocks inside methods that these things are pretty independent and better handled by a method call which has the added benefit of providing documentation via the method name, if chosen wisely. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/