by rowborg · 13y ago · 0 comments
if (null != foo);
bar();
This is valid code in C and Java, and bar() will always run in this case.Having seen people waste hours on such a semicolon, I always use braces, even in one-liners, because I never know when someone is going to break it out into multiple lines later:
if (null != foo) { bar(); }It does however indent the line after the semicolon to the same level as the if, which is at least a red flag that something is up, if you are used to how the auto-indenting normally works.
When I've seen this happen, it wasn't because of a semicolon added when the code was written. It was someone accidentally adding a semicolon to a line later, without realizing it. Unless they then went to the next line and hit the "fix indentation" key, they didn't catch it.
if (null != foo);
{
bar();
}http://en.wikipedia.org/wiki/Indent_style#Variant:_1TBS
since your eyes would flag:
if (null != foo); {
bar();
}
as badness.[edit: correct } to {, ta]
Because I've gotten used to Go's support for short assignment in an if, the semi doesn't look completely out of place there (though the construction here is not valid Go either).
if (null != foo) bar();
or if (null != foo) {
bar();
}More seriously, after more than 10 years of writing C, C++ and Java, I've never run into this problem. I still wrap mine out of habit (to prevent this dreaded occurrence), but I think good testing would obviate any need for this.