# 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);

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

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