Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make eclipse automatically add braces to an IF statement?

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?

like image 438
Bromide Avatar asked Sep 13 '10 20:09

Bromide


People also ask

How do you align braces in eclipse?

The simplest and global way: Go to Window -> Preferences. Then in search put: "brace". Select -> Java -> Code Style-> Formatter After opening new window go to brace tab and change it as you prefer.

How do I fix brackets in Eclipse?

In your case: Window->Preferences->Keys -> ctrl+alt+b Unbind Command should do the trick. It also the same in Spring Tool Suite 4(It is an Eclipse-based development environment.)

Does IF statement need braces Java?

If the true or false clause of an if statement has only one statement, you do not need to use braces (also called "curly brackets"). This braceless style is dangerous, and most style guides recommend always using them.

How do you check curly braces in eclipse?

To see a bracket's pair, click to the right of the bracket. Its matching pair will be highlighted. To jump to the matching bracket, press Ctrl+Shift+P.


1 Answers

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

like image 114
gk5885 Avatar answered Sep 18 '22 04:09

gk5885