# Add Braces to Control Statements

# Properties

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

# 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 read helps avoid such errors and improves readability.

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

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow version:

# Tags

1
default
You & jSparrow
default

Hey there! May I help you? 😊