Rob said:
Functionally, the two following pieces of code are the same:
if(p == NULL) {
break;
}
if(p == NULL)
break;
However, I am wondering whether there are any performance implications
in choosing one over the other. Is there any extra overhead created by
defining a block? Will a compiler optimize the former code to the
latter? Does it even need to?
The reason I ask is, I'd much prefer to go with the former code, simply
because I prefer the appearance.
As others have pointed out, the difference in terms of efficiency is
negligible.
As far as I know, all languages in the Pascal tradition after Pascal do
require explicit termination of control statements (we're talking 1970's
here). What seemed to be a good idea at first -- the distinction between
statements and statement sequences in control statements (e.g. Algol,
Pascal and C) -- turned out not to be, so I see no reason why ANSI/ISO
never decided to make braces mandatory in C.
As I see it, the advantages of explicit control statement termination are:
* Bugs are avoided (multiple statements on one line, the classical
else-if situation, wrong indentation...)
* If you use braces only when necessary, you have to decide if they are
required or not at every control statement you write. If you add one
statement to a single statement you have to add the braces, and if you
remove one of two statements you probably want to remove the braces as
well to achieve consistency throughout the code. This requires some
extra editing. Since all choices in programming are distracting, the
irrelevant ones should be removed.
* With explicit control statement termination the language gets less
complicated and more regular.
Disadvantage:
* The code gets somewhat more cluttered and slightly less readable in
case of short control statements.
August