# Add Braces to Control Statements

# Description

Checks if braces are used to encapsulate control statements and adds them if they aren't present.

# Benefits

While not technically incorrect, the omission of curly braces can be misleading, and may lead to the introduction of errors during maintenance. Applying this rule helps avoid such errors and improves readability.

# Tags

# Code Changes

# if

Pre

if (a < b) foo();

Post

if (a < b) { foo(); }

# else

Pre

else bar();

Post

else {
    bar();
}

# else if

Pre

else if (a >= b) bar();

Post

else if (a >= b) {
    bar();
}

# for

Pre

boolean petal = true;
for (int i = 0; i < 10; i++) petal = !petal;

Post

boolean petal = true;
for (int i = 0; i < 10; i++) {
    petal = !petal;
}

# while

Pre

int cnt = 0;
while (cnt < 10) cnt++;

Post

int cnt = 0;
while (cnt < 10) {
    cnt++;
}

# do while

Pre

int cnt = 0;
do cnt++; while (cnt < 10);

Post

int cnt = 0;
do {
    cnt++;
} while (cnt < 10);

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID BracketsToControl
First seen in jSparrow version 1.0.0
Minimum Java version 1.1
Remediation cost 2 min
Links