Java Reference
In-Depth Information
Click here to view code image
public void assignNocontrol(BufferedReader reader)
throws IOException {
String line;
while ((line = reader.readLine()) != null) {
// ... Work with line
}
}
Bibliography
[Hatton 1995]
§2.7.2, “Errors of Omission and Addition”
54. Use braces for the body of an if , for , or while statement
Use opening and closing braces for if , for , and while statements even when the body
contains only a single statement. Braces improve the uniformity and readability of code.
More important, it is easy to forget to add braces when inserting additional statements
into a body containing only a single statement, because the conventional program indent-
ation gives strong (but misleading) guidance to the structure.
Noncompliant Code Example
This noncompliant code example authenticates a user with an if statement that lacks
braces.
int login;
if (invalid_login())
login = 0;
else
login = 1;
This program behaves as expected. However, a maintainer might subsequently add a
debug statement or other logic but forget to add opening and closing braces:
Click here to view code image
Search WWH ::




Custom Search