
〔Coding style〕Reasons to add the curly brackets behind if / for / while in C/C++ - goblinbear
https://medium.com/@goblin5417/coding-style-reasons-to-increase-the-brackets-behind-if-for-while-617e6665ca97
======
pwdisswordfish2
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;”).

------
mrgriffin
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.

------
nwmcsween
counterpoint: having to scroll around to understand the code with superfluous
syntax makes understanding harder and slower as you have to jump around.

