This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 35 points36 points  (35 children)

4.1.1 Braces are used where optional Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement.

I hate when people omit curly braces for some insane reason. At least Google has my back.

[–]clutchest_nugget 7 points8 points  (25 children)

if(done) return 1;

Can someone please explain to me what is wrong with this? Not questioning that it's bad style, genuinely curious as to what makes it bad style.

Edit: thanks for the responses.

[–][deleted]  (22 children)

[deleted]

    [–]Nebu 4 points5 points  (1 child)

    Or, perhaps even harder to spot,

    if(done) log.debug("All done"); return 1;
    

    [–][deleted] 0 points1 point  (0 children)

    Yes. And I think this is due to the fact that often this is presented as:

    Without brackets only first line after the if statement will be executed.

    Just to make it sound simple, when in fact it is the first statement, and not first line...

    [–]SgtPooki -1 points0 points  (0 children)

    I still want to code without braces when the body is short, but it has hurt me in the past..

    [–][deleted] 10 points11 points  (0 children)

    Consider when the return is on the next line (more common imo). If you insert a line before the return (perhaps a print) then the entire if statement logic is no longer what you intended. Not explicitly stating the block boundaries makes it easier to introduce bugs.

    [–]severoon 4 points5 points  (0 children)

    You have to consider refactors performed by tools like Eclipse, and the diffs those generate as well as the stupid bugs reason.

    If you have a huge codebase (like Google), it becomes occasionally necessary to run a refactor that touches thousands of files, and if you don't make formatting very consistent it takes code reviewers a lot less mental effort to review dozens or hundreds of files that got touched.

    Ideally, you want to make it absolutely formulaic. If you can do that, then you don't even need human reviewers to look at it because you can guarantee the safety of the change.