Louis I. answered 06/10/19
Computer Science Instructor/Tutor: Real World and Academia Experienced
Well, it's mainly an issue of code clarity/consistency and simplifying code modification.
I for one, wish that the single-statement code block {} exception was not permitted in the language ... it doesn't buy us much - in fact, I think it generates more confusion than benefit, and led to this rather ambiguous "best practice" that you're asking about now.
I've wondered if Dennis Richie felt the same way in retrospect ;-)
Anyway, a rapidly evolving program often expands on logical and repetitive code blocks, perhaps increasing the number of statements in the block from 1 to >1.
It's simply more straightforward and less prone to error to go FROM:
if( condition ) {
statement;
}
while( condition ) {
statement;
}
TO
if( condition ) {
statement1;
statement2;
}
while( condition ) {
statement1;
statement2;
}
THAN FROM
if( condition )
statement;
while( condition )
statement;
TO
if( condition ) {
statement1;
statement2;
}
while( condition ) {
statement1;
statement2;
}
A developer might inadvertently re-code to something like this:
if( condition )
statement1;
statement2; // intended to be part of if block - introducing a subtle bug - statement2 will execute unconditionally ....
Might the chances of making this mistake be lower if the { } were always used to implement a code block?
I think so!