2

For example, building a GUI in the Swing library, you often need to build a few parts within a JPanel.

eg.

public CustomPanel extends JPanel {

    public CustomPanel() {
        super();

        // Build the slider.
        {
            ...
        }

        // Build the combo boxes.
        {
            ...
        }
    }
}

It seems the following are my options:

  • Use the {} the way I am currently using them (basically for indentation of similar code).
  • Create custom methods for each component eg. public JSlider createCustomSlider().
  • Create custom classes for each custom component (this seems a bit overkill if I have a number of one off components).

Is there a downside to how I indent my code with {}?

EDIT:

Also, as an additional benefit, the braces currently serve to scope my variables. That means I'm always certain that I don't accidentally use them further down the line.

sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 2
    methods are a little more declarative and can be reusable.. – nachokk Jul 09 '13 at 21:33
  • possible duplicate of [Efficiency of Java "Double Brace Initialization"?](http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization) – Nir Alfasi Jul 09 '13 at 21:36
  • @alfasin No, that's about initializing data structures (edit: with values) when you declare them. – sdasdadas Jul 09 '13 at 21:36
  • @sdasdadas I doubt there's any difference between using the double brace paradigm as an initializer vs. anywhere else in the code. – Nir Alfasi Jul 09 '13 at 21:38
  • @alfasin http://stackoverflow.com/questions/17558977/does-using-braces-inside-a-java-method-reduce-performance – sdasdadas Jul 09 '13 at 21:56

1 Answers1

4

I'd use methods. Having brackets doesn't add anything to the documentation that you don't already have in your comments, having well named methods is always a good idea.

Downside of your method -- you'll be the only one who understands why you did it that way.

John Lockwood
  • 3,787
  • 29
  • 27
  • He said that he also uses it to make sure he won't use "local" parameters out of scope! – Nir Alfasi Jul 09 '13 at 21:39
  • 1
    @alfasin Out of the scope of where they are currently being used. There's a ton of literature that shows that errors increase as the distance of use from variable declaration increases. – sdasdadas Jul 09 '13 at 21:44
  • @alfasin I was under the (false?) impression that you didn't regard my scope comment as a positive to using braces. – sdasdadas Jul 09 '13 at 21:47
  • @sdasdadas right, and that's exactly why you should use methods (which also opens a new frame on the stack - exactly like braces) - for clarity and easy maintenance of your code! – Nir Alfasi Jul 09 '13 at 21:47
  • @sdasdadas I didn't mean to sound sarcastic, I was just stating (on the first comment) that John disregarded one of the key reasons you introduced. – Nir Alfasi Jul 09 '13 at 21:48
  • Ok, that was my mistake then. I'll probably end up accepting this answer - I'm just trying to found out if this is different from double brace initialization. Thanks John, I do understand that methods are probably the way to go. – sdasdadas Jul 09 '13 at 21:49
  • @sdasdadas: Yes, code blocks and double brace initialization are completely different. Double brace initialization means creating a subclass of a type (that's the reason for the first brace pair) and creating an init block for initializing the variables in it (this is the inner pair of braces). – Igor Rodriguez Jul 09 '13 at 22:07