Just out of curiosity, I checked the Google C++ style guide, but it doesn't really help:
In general, curly braces are not required for single-line statements, but they are allowed if you like them;
When I first started doing firmware, I was kind of annoyed having to go back and remove the braces from single line statements. It's kind of muscle memory to add them. I also don't have to think up front if I need them or not. And to Vadim's point, adding debug statements into a block is less error prone and less work if the braces are already there. For me, reading a diff where debug prints are added is easier if the braces were already there. Then I don't have to concern myself that I changed the condition.
e.g.,
/* Default to 4 KiB search space. */
if (req_size == 0)
if (req_size == 0) {
debug("Using default size\n"); req_size = 4 * 1024;
}
vs
/* Default to 4 KiB search space. */ if (req_size == 0) {
debug("Using default size\n"); req_size = 4 * 1024; }
But at the same time, I've grown accustomed to reading code like this:
if (table == NULL) return -1;
I guess I would opt for requiring braces.
On Tue, Jun 25, 2019 at 3:17 PM Vadim Bendebury vbendeb@chromium.org wrote:
On Tue, Jun 25, 2019 at 1:29 PM Julius Werner jwerner@chromium.org wrote:
The improvement of requiring a { on the ifs is known to
have positive impact; it's why Rust and Go both require it to my understanding.
How is this actually "known"?
Mandatory brackets are especially helpful when one makes quick debugging changes, say adding a print statement or an assignment and forgets to add the brackets to single statement clauses.
The compiler option would be useless if the debug code does not follow the indentation pattern.
-vb
coreboot mailing list -- coreboot@coreboot.org To unsubscribe send an email to coreboot-leave@coreboot.org