53

In Java the following is completely valid:

if (x == null)
    Y();
else
    Z();

I personally don't like it at all. I like all my IF statements to have braces:

if (x == null) {
    Y();
} else {
    Z();
}

The eclipse formatter is wonderful and can beautify my code in many other ways.

Is there a way to have it add the braces to IF statements?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Bromide
  • 1,102
  • 1
  • 10
  • 23
  • 1
    As a side note, people who use Allman style braces seem to be the ones to prefer no braces for simple single line blocks after very readable control statements. It has less ceremony for that usual situation with a trade off of an extra line for more complicated situations. http://en.wikipedia.org/wiki/Indent_style – Peter DeWeese Sep 13 '10 at 21:17
  • This is basically the same question as http://stackoverflow.com/questions/1236531/eclipse-and-curly-braces – David Tonhofer Feb 05 '13 at 22:39
  • 4
    No it isn't - this question is asking about enforcing braces for single line if/else clauses, whereas http://stackoverflow.com/questions/1236531/eclipse-and-curly-braces is asking about how to enforce a particular layout of existing braces. – SamStephens Mar 09 '13 at 02:07

5 Answers5

85

Under "Preferences": Java > Editor > Save Actions

1) Check "Additional actions"

2) Click "Configure…"

3) Go to the "Code Style" tab

4) Check "Use blocks in if/while/for/do statements" and configure to your preferences

gk5885
  • 3,742
  • 21
  • 16
  • 4
    Which version of eclipse are you using? it doesn't seem to work with me – Pacerier Oct 19 '11 at 05:53
  • 1
    I dont know why, but my brain kept forcing me to look for this setting in Java->code style > formatter. This is totally a different section. Thanks :) – Manish Bansal Dec 30 '19 at 06:42
  • This is not a valid solution anymore (obsolete) and Eclipse is not what is used to be 10 years ago. ;) – Gabriel Petrovay Feb 22 '21 at 11:58
  • In 2022, with 4.22 (2021-12), in Preferences -> Java -> Editor -> Save Actions, enable Additional Actions and click Configure. Choose the Code Style tab (2nd). Right at the top is Use blocks in if/while/for/do statements; select it and choose the Always button below it. Comments after a naked if go away. – Alan Jan 20 '22 at 20:36
22

Yes.

Eclipse menu: Source -> Clean Up...

Configure... -> Code Style -> Use blocks in if/while/for/do statements.

ChrisH
  • 4,788
  • 26
  • 35
  • 2
    I liked this option more than the @gk5885's one... sorry :) A word of caution, Eclipse will delete the comments you had after the if. – ArturoTena Dec 30 '14 at 18:43
  • This option seems to have disappeared from Eclipse Oxygen (2017). – simon Jul 27 '17 at 10:53
  • 1
    What is the property name for that to find in formatter xml file ? – MAK Ripon Oct 21 '20 at 02:59
  • I really need to know if this 'preference' can be inserted into an Eclipse formatter XML file, as I use NetBeans with an [External Formatter](https://github.com/funfried/externalcodeformatter_for_netbeans/) and I need to conform to the majority of my colleagues whom are Eclipse addicts...except...this is just one option my OCD cannot tolerate...missing braces! – Darrin Jun 23 '23 at 18:10
10

In Eclipse Oxygen (2017+), that option is now at:

Window -> Preferences -> Java -> Code Style -> Clean Up -> Edit -> second tab "Code Style"

After changing, select the corresponding parts of your code and run the "Clean Up" option.

user1050755
  • 11,218
  • 4
  • 45
  • 56
0

This is what I did: Eclipse -Preferences-Java-Code Style-Formatter-Edit-Braces Select next line as desired in each box then save as a different name (your choice).

Peter
  • 9
  • 1
-7

I usually do it by creating a code formatter by copying the built in eclipse formatter, here:

Windows > Preferences > Java > Code Style > Formatter 

Select the profile that you have just created. and click on Edit and go to "Braces" tab.

Change the braces placement as you like (it's a WYSIWYG kind of editor, makes it easy) .

When I need to format, I just select all the text and Ctrl+Shift+F and it works fine for me.

Ayusman
  • 8,509
  • 21
  • 79
  • 132