47

For years, I've been using named blocks to limit the scope of temporary variables. I've never seen this done anywhere else, which makes me wonder if this is a bad idea. Especially since the Eclipse IDE flags these as warnings by default.

I've used this to good effect, I think, in my own code. But since it is un-idiomatic to the point where good programmers will distrust it when they see it, I really have two ways to go from here:

  1. avoid doing it, or
  2. promote it, with the hope that it will become an idiom.

Example (within a larger method):

final Date nextTuesday;
initNextTuesday: {
    GregorianCalendar cal = new GregorianCalendar();
    ... // About 5-10 lines of setting the calendar fields
    nextTuesday = cal.getTime();
}

Here I'm using a GregorianCalendar just to initialize a date, and I want to make sure that I don't accidentally reuse it.

Some people have commented that you don't actually need to name the block. While that's true, a raw block looks even more like a bug, as the intent is unclear. Furthermore, naming something encourages you to think about the intention of the block. The goal here is to identify distinct sections of code, not to give every temporary variable its own scope.

Many people have commented that it's best to go straight to small methods. I agree that this should be your first instinct. However, there may be several mitigating factors:

  • To even consider a named block, the code should be short, one-off code that will never be called elsewhere.
  • A named block is a quick way to organize an oversized method without creating a one-off method with a dozen parameters. This is especially true when a class is in flux, and the inputs are likely to change from version to version.
  • Creating a new method encourages its reuse, which may be ill-advised if the use cases aren't well-established. A named block is easier (psychologically, at least) to throw away.
  • Especially for unit tests, you may need to define a dozen different objects for one-off assertions, and they are just different enough that you can't (yet) find a way to consolidate them into a small number of methods, nor can you think of a way to distinguish them with names that aren't a mile long.

Advantages of using the named scope:

  1. Can't accidentally reuse temporary variables
  2. Limited scope gives garbage collector and JIT compiler more information about programmer intent
  3. Block name provides a comment on a block of code, which I find more readable than open-ended comments
  4. Makes it easier to refactor code out of a big method into little methods, or vice versa, since the named block is easier to separate than unstructured code.

Disadvantages:

Not idiomatic: programmers who haven't seen this use of named blocks (i.e. everyone but me) assume it's buggy, since they can't find references to the block name. (Just like Eclipse does.) And getting something to become idiomatic is an uphill battle.

It can be used as an excuse for bad programming habits, such as:

  • Making huge, monolithic methods where several small methods would be more legible.
  • Layers of indentation too deep to read easily.

Note: I've edited this question extensively, based on some thoughtful responses. Thanks!

David Leppik
  • 3,194
  • 29
  • 18
  • 2
    You don't actually need a label on the block. – Tom Hawtin - tackline Oct 15 '08 at 16:48
  • This is unrelated, but doesn't Sun recommend using Calendar cal = Calendar.getInstance(); ? – Powerlord Oct 15 '08 at 19:54
  • 1
    About it being non-idiomatic: not everything must be idiomatic. For a style to be idiomatic, it must be useful in a situation that often comes up. However, what you described is a perfectly optimal solution for a very unusual situation (a code block that sort of does it's own thing, but is too small/non-reusable to be an own method.) No, not everything needs to be it's own method, too many methods is just as spaghetti as too few. So in conclusion, if you are pro enough to feel out this exact situation that benefits from this, keep using the named blocks, otherwise don't. – Cray Oct 11 '12 at 21:11
  • Another note: to me, an intent of an unnamed block is much more clear than of a named one. An unnamed block just says "scope limiting". A name just creates more confusion and induces search for continue/break statements inside that block, and feeling of unprofessional code if there aren't any. – Cray Oct 11 '12 at 21:14
  • Eclipse (*by default*) will warn you that **"The label initNextTuesday is never explicitly referenced"**. Although the warning can be turned off, most developers don't personalize Eclipse and will likely see yellow squiggles. I personally think comments explaining that you're limiting scope is better than labels on the block. – ADTC Sep 12 '14 at 10:59

14 Answers14

26

I'd just go straight for refactoring into smaller methods. If a method is big enough that it needs breaking up like this, it really needs breaking up into multiple methods if at all possible.

While limiting scope is nice, this isn't really what named blocks are for. It's unidiomatic, which is very rarely a good thing.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    What are named blocks for specifically? – Jeremy Oct 15 '08 at 16:51
  • I've only ever seen them used for break/continue in loops. – Jon Skeet Oct 15 '08 at 16:59
  • 2
    Here "named blocks" refers to a block with a label. You can put a label on any statement, however it (currently) only makes sense for a block or control statement. Labels can be appended to break and continue statements to specify which (indirectly) enclosing statement they apply to. – Tom Hawtin - tackline Oct 15 '08 at 19:04
17

If this was bad, then why is this a feature in the language! It's got a purpose, and you've found it.

I often write code exactly as in your example. When you want to initialize a variable, and there's a little calculation that needs doing to work out what that should be, and that involves a couple of variables... then you don't want those variables hanging around for the entire scope of your function, then a little scope to contain the initialization works great.

Mini scopes are an easy way to break code into 'paragraphs'. If you split into methods you can make the code harder to navigate when those methods don't get called from anywhere else and have a serial kind of order in which they need to be executed.

It's always a balance, but if you think it's going to be easiest to maintain and it actually adds value to a future reader of your code if its all inline, then go for it.

There are no hard and fast rules. I get a little fed up sometimes with co-workers who excessively put everything into its own method or class or file, and this becomes a nightmare to navigate. There's a nice balance somewhere!

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • 1
    I agree. I think we only supposed to create a method if it is called or can be called from multiple places or from the same place multiple times with different arguments. To me it's very confusing if there's a method with the sole purpose of structuring a longer method to several shorter ones. It's clearer to have a longer method if it has only one purpose. If two methods have very distinctive purposes then they should be separate of course. – Ádám Bozzay Jan 05 '21 at 14:01
10

Sometimes I use unnamed blocks to isolate mutable things needed to prepare some immutable thing. Instead of having a label I put the Block under the immutable variable declaration.

final String example;
{
   final StringBuilder sb = new StringBuilder();
   for(int i = 0; i < 100; i++)
     sb.append(i);
   example = sb.toString();

}

When I find some other use for the block, or just think that it's in the way, I turn it into a method.

John Nilsson
  • 17,001
  • 8
  • 32
  • 42
9

Using blocks to limit scope is a good technique in my book.

But since you're using the label to do the work of a comment, why not just use an actual comment instead? This would remove the confusion about the unreferenced label.

5

This is the 1st time I am seeing someone else using blocks. whew! I thought I was the only one. I know that I didn't invent it -- remembered reading it somewhere -- possibly from my previous C++ world.

I don't use the labels, though and just comment what I'm doing.

I don't agree with all the guys that are asking you extract it into a method. Most of the things we don in such blocks aren't really reusable blocks. It makes sense in a big initialization AND YES, I've used blocks to prevent COPY/PASTE errors.

BR,
~A

anjanb
  • 12,999
  • 18
  • 77
  • 106
3

If you have 5-10 lines of code that can safely be put into a block like that, the same code could just as well be extracted into a method.

This might seem like it's only a semantic difference, but at least with extracting into a method then you would gain the benefit of the ability of re-use.

matt b
  • 138,234
  • 66
  • 282
  • 345
2

Just because they exist doesn't mean they should be used. Most of the advantages gained from using named blocks are better gained by using a new private method.

  1. You won't be able to use the temporary variables declared in the new method
  2. The GC and JIT Compiler will glean the same info by using a new method
  3. Using a descriptive name for the new method (using "private Date initNextTuesday()" in your case) will allow for the self commenting code advantage
  4. No need to refactor code when you have already "pre-factored" it

In addition to these benefits, you also get code reuse benefits and it will shorten your long methods.

Haphazard
  • 10,900
  • 6
  • 43
  • 55
2

I'd use a block with a comment rather adding a label there.

When I see a label, I can't assume that nothing else is referencing the block.

If I change the behavior of the block, then the label name may not be appropriate any more. But I can't just reach out and change it: I'll have to look through the rest of the method to determine what label is calling out to the block. At which point I'll figure out that it's an unreferenced label.

Using a comment is clearer in this instance, because it describes the behavior of the block without imposing any extra work on the part of the maintainer.

Will Sargent
  • 4,346
  • 1
  • 31
  • 53
1

It's a good technique in my book. Managing large numbers of throwaway methods is evil and the reasons you're providing for naming the blocks are good.

What does the generated bytecode look like? That'd be my only hesitation. I suspect it strips away the block name and might even benefit from greater optimizations. But you'd have to check.

Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
1

Sorry for resurrecting this, but I didn't see anyone mention what I consider to be a very important point. Let's look at your example:

final Date nextTuesday;
initNextTuesday: {
    GregorianCalendar cal = new GregorianCalendar();
    ... // About 5-10 lines of setting the calendar fields
    nextTuesday = cal.getTime();
}

Including this initialization logic here makes it easier to understand if you're reading the file from top to bottom and care about every line. But think about how you read code. Do you start reading from the top of a file and continue to the bottom? Of course not! The only time you would ever do that is during a code review. Instead, you probably have a starting point based on previous knowledge, a stack trace, etc. Then you drill further down/up through the execution path until you find what you're looking for. Optimize for reading based on execution path, not code reviews.
Does the person reading the code that uses nextTuesday really want to read about how it's initialized? I would argue that the only information that they need is that there's a Date corresponding to next Tuesday. All of this information is contained in its declaration. This is a perfect example of code that should be broken into a private method, because it isn't necessary to understand the logic that the reader cares about.

final Date nextTuesday;
initNextTuesday: {
    GregorianCalendar cal = new GregorianCalendar();
    //1
    //2
    //3
    //4
    //5
    nextTuesday = cal.getTime();
}

vs:

final Date nextTuesday = getNextTuesday();

Which would you rather read on your way through a module?

Floegipoky
  • 3,087
  • 1
  • 31
  • 47
  • I guess what you mean is long code should go into method, while the original post says short code should stay in a block. Both looks good for me. – jin Apr 20 '15 at 23:38
1

Name Blocks helps: Using break as a Form of Goto

Using break as a civilized form of goto.

class Break {
    public static void main(String args[]) {
        boolean t = true;
        first: {
            second: {
                third: {
                    System.out.println("Before the break.");
                    if (t)
                        break second; // break out of second block
                    System.out.println("This won't execute");
                }
                System.out.println("This won't execute");
            }
            System.out.println("This is after second block.");
        }
    }
}

Using break to exit from nested loops

class BreakLoop4 {
    public static void main(String args[]) {
        outer: for (int i = 0; i < 3; i++) {
            System.out.print("Pass " + i + ": ");
            for (int j = 0; j < 100; j++) {
                if (j == 10)
                    break outer; // exit both loops
                System.out.print(j + " ");
            }
            System.out.println("This will not print");
        }
        System.out.println("Loops complete.");
    }
}

Source Link

Premraj
  • 72,055
  • 26
  • 237
  • 180
0

I have done this in some of my c#. I didn't know you could name the blocks though, I'll have to try that see if it works in c# too.

I think the scope block can be a nice idea, because you can encapsulate code specific to something within a block of code, where you might not want to split it out into its own function.

As for the disadvantage of nesting them, I see that as more of a fault of a programmer not of scope blocks themselves.

Jeremy
  • 44,950
  • 68
  • 206
  • 332
0

Named scopes are technically ok here, it's just they aren't used in this way very often. Therefore, when someone else comes to maintain your code in the future it may not be immediately obvious why they are there. IMHO a private helper method would be a better choice...

johnstok
  • 96,212
  • 12
  • 54
  • 76
0

I love the idea of using block to limit var scope. So many times I was confused by short-lived vars given large scope which should go away immediately after use. Long method + many non-final vars make it difficult to reason about the coder's intention, especially when comments are rare. Considering much of the logic I see in a method were like below

Type foo(args..){
    declare ret
    ...
    make temp vars to add information on ret
    ...

    make some more temp vars to add info on ret. not much related to above code. but previously declared vars are still alive
    ...


    return ret
}

if vars can have smaller scope than the entire method body, I can quickly forget most of them (good thing).

Also I agree that too many or too few private things leads to spaghetti code.

Actually what I was looking for was something like nested method in functional languages, and seems its cousin in Java is a {BLOCK} (inner class and labmda expression are not for this..).

However, I would prefer to use a unnamed block since this may be misleading to people trying to find the reference to the label, plus I can explain better with commented block.

For using a private method, I would consider it as the next step of using blocks.

jin
  • 1,542
  • 1
  • 9
  • 10