Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #11366

Re: Blocks for scope control

From Arved Sandstrom <asandstrom3minus1@eastlink.ca>
Newsgroups comp.lang.java.programmer
Subject Re: Blocks for scope control
References <dBCQq.122$d%2.16@newsfe07.iad> <jev7op$j4k$1@news.albasani.net>
Message-ID <IRHQq.2650$2C.1194@newsfe20.iad> (permalink)
Organization Public Usenet Newsgroup Access
Date 2012-01-15 17:49 -0400

Show all headers | View raw


On 12-01-15 03:00 PM, Lew wrote:
>  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?
> 
> You see it standardly in initialization blocks, especially static,
> though I often (more often than not) find myself refactoring static
> initialization blocks to private static methods.  For instance
> initialization they're extremely useful for blocks that multiple
> constructors must share.

At the instance and class levels, it's true, these blocks operate
similarly to "local" blocks.

> Within methods I use them as well, to enforce temporariness of
> variables. This is very handy to prevent re-use of a variable whose
> usefulness has expired.  The need is rare, but like the legendary
> left-handed stembolt, if you don't have it when you need it, that's a
> worry.

This is exactly the use I was calling out. After visibility modifiers we
then have a number of levels of scope - class, instance, method and
block. We already use restrictive variable scoping in blocks; everyone
who re-uses for-loop index names benefits from this.

My point is simply that on some occasions, as you point out yourself,
you do want to "enforce temporariness" of a variable in a scope narrower
than that of its enclosing method. The answer here, on those infrequent
occasions where it makes sense to use a block, is not to blindly create
a new method simply to get variable scoping. That reflexive practice can
lead to unreadable fragmented code.

> This idiom is also valid in other languages (e.g., C).
> 
> Calling such code blocks "anonymous" doesn't make sense.  Code blocks
> aren't named in the first place.

Well, to be fair, *I* didn't call them "anonymous", the original poster
at StackOverflow did. :-) Strictly speaking they are blocks, and nothing
but blocks; in order to specify more exactly what type of block I meant
I referred to them as "local blocks".

> There are many, many idioms in Java that are woefully underused. 
> 'assert' is the prime example.  People don't understand it, even
> excoriate it (strange as that seems), and it's been around about a
> decade, since Java 1.4.  Lack of use does not equate to lack of value.
> 
Quite apart from (lack of) understanding as to how to apply assertions
effectively and correctly, I believe that there are other reasons why
they are not more widely used. Some are good, some not so good, some are
neutral.

I'm with you on this one, though. I like assertions and use them.

AHS
-- 
...wherever the people are well informed they can be trusted with their
own government...
-- Thomas Jefferson, 1789

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-15 11:50 -0400
  Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-15 17:10 +0100
  Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-15 17:32 +0100
    Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 11:21 -0800
    Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-16 07:59 +0100
  Re: Blocks for scope control markspace <-@.> - 2012-01-15 09:57 -0800
    Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 11:28 -0800
  Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 11:00 -0800
    Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-15 17:49 -0400
      Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-15 15:45 -0800
        Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-19 21:54 -0400
          Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-20 08:41 +0100
            Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-20 15:24 -0800
            Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-20 19:30 -0400
              Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-20 15:54 -0800
      Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-16 07:55 +0100
        Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-19 21:16 -0400
          Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-21 13:25 +0100
            Re: Blocks for scope control Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-21 10:41 -0400
              Re: Blocks for scope control Robert Klemme <shortcutter@googlemail.com> - 2012-01-22 13:03 +0100
  Re: Blocks for scope control Roedy Green <see_website@mindprod.com.invalid> - 2012-01-16 06:09 -0800
    Re: Blocks for scope control Henk van Voorthuijsen <voorth@xs4all.nl> - 2012-01-16 07:03 -0800
  Re: Blocks for scope control Arne Vajhøj <arne@vajhoej.dk> - 2012-01-17 18:43 -0500
  Re: Blocks for scope control Lew <noone@lewscanon.com> - 2012-01-20 11:05 -0800
  Re: Blocks for scope control Gene Wirchenko <genew@ocis.net> - 2012-01-20 12:11 -0800
  Re: Blocks for scope control Arne Vajhøj <arne@vajhoej.dk> - 2012-02-06 21:24 -0500

csiph-web