Multi-line macros should be wrapped in “do { ... } while (0)” anyway. Not only does it ensure that control flow constructs apply to them atomically as intended, regardless if they use curly brackets or not, but also isolates any potential variable declarations into their own scope, ensures that all uses of the macro are followed by a semicolon and gives you an early exit construct for free (“break;”).
I don't write brackets where the condition and body fit on one line, and do otherwise. e.g. like these:
if (ptr == NULL) return NULL;
for (int i = 0; i < n; ++i) dst[i] = src[i];
I particularly like it for guards at the beginning of a function, because they end up being less visually heavy, thus drawing my attention to the (presumably more business-logic-y) other lines of code.
It's somewhat like how Ruby lets you write your control structure at the end of a statement to have it only apply to that statement, e.g.
return nil if ptr.nil?
And I find this style looks different enough to multiple statement control structures that I don't make the mistake of adding an additional statement without also adding brackets.
Of course this is a comment about formatting code, so YMMV.
3 comments
[ 3.4 ms ] story [ 21.9 ms ] threadIt's somewhat like how Ruby lets you write your control structure at the end of a statement to have it only apply to that statement, e.g.
And I find this style looks different enough to multiple statement control structures that I don't make the mistake of adding an additional statement without also adding brackets.Of course this is a comment about formatting code, so YMMV.