Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

88%
+13 −0
Q&A Is omitting braces for single statements bad practice?

Not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, CERT C EXP19-C and others). Once upon a time I liked to skip out braces too, bu...

posted 4y ago by Lundin‭  ·  edited 3y ago by Lundin‭

Answer
#4: Post edited by user avatar Lundin‭ · 2021-03-10T14:44:21Z (over 3 years ago)
Updated link
  • Not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://www.securecoding.cert.org/confluence/x/1QGMAg) and others).
  • Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.
  • The CERT-C link above gives an excellent example:
  • if (invalid_login())
  • if (allow_guests())
  • privileges = GUEST;
  • else
  • privileges = ADMINISTRATOR;
  • The else here ties to the inner if statement. Oops.
  • ---
  • It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they often cause more bugs than they fix. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:
  • if(x) {}
  • foo();
  • }
  • And if I don't notice that extra } and continue to type:
  • if(x) {}
  • if(y) {
  • foo();
  • }
  • Not quite what I intended.
  • Not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://wiki.sei.cmu.edu/confluence/display/c/EXP19-C.+Use+braces+for+the+body+of+an+if%2C+for%2C+or+while+statement) and others).
  • Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.
  • The CERT-C link above gives an excellent example:
  • if (invalid_login())
  • if (allow_guests())
  • privileges = GUEST;
  • else
  • privileges = ADMINISTRATOR;
  • The else here ties to the inner if statement. Oops.
  • ---
  • It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they often cause more bugs than they fix. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:
  • if(x) {}
  • foo();
  • }
  • And if I don't notice that extra } and continue to type:
  • if(x) {}
  • if(y) {
  • foo();
  • }
  • Not quite what I intended.
#3: Post edited by user avatar Lundin‭ · 2020-10-30T16:13:27Z (about 4 years ago)
  • Not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://www.securecoding.cert.org/confluence/x/1QGMAg) and others).
  • Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.
  • The CERT-C link above gives an excellent example:
  • if (invalid_login())
  • if (allow_guests())
  • privileges = GUEST;
  • else
  • privileges = ADMINISTRATOR;
  • The else here ties to the inner if statement. Oops.
  • ---
  • It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they cause more bugs than they fix in my experience. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:
  • if(x) {}
  • foo();
  • }
  • And if I don't notice that extra } and continue to type:
  • if(x) {}
  • if(y) {
  • foo();
  • }
  • Not quite what I intended.
  • Not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://www.securecoding.cert.org/confluence/x/1QGMAg) and others).
  • Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.
  • The CERT-C link above gives an excellent example:
  • if (invalid_login())
  • if (allow_guests())
  • privileges = GUEST;
  • else
  • privileges = ADMINISTRATOR;
  • The else here ties to the inner if statement. Oops.
  • ---
  • It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they often cause more bugs than they fix. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:
  • if(x) {}
  • foo();
  • }
  • And if I don't notice that extra } and continue to type:
  • if(x) {}
  • if(y) {
  • foo();
  • }
  • Not quite what I intended.
#2: Post edited by user avatar Lundin‭ · 2020-10-30T16:12:24Z (about 4 years ago)
  • Yes, not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://www.securecoding.cert.org/confluence/x/1QGMAg) and others).
  • Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.
  • The CERT-C link above gives an excellent example:
  • if (invalid_login())
  • if (allow_guests())
  • privileges = GUEST;
  • else
  • privileges = ADMINISTRATOR;
  • The else here ties to the inner if statement. Oops.
  • ---
  • It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they cause more bugs than they fix in my experience. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:
  • if(x) {}
  • foo();
  • }
  • And if I don't notice that extra } and continue to type:
  • if(x) {}
  • if(y) {
  • foo();
  • }
  • Not quite what I intended.
  • Not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://www.securecoding.cert.org/confluence/x/1QGMAg) and others).
  • Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.
  • The CERT-C link above gives an excellent example:
  • if (invalid_login())
  • if (allow_guests())
  • privileges = GUEST;
  • else
  • privileges = ADMINISTRATOR;
  • The else here ties to the inner if statement. Oops.
  • ---
  • It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they cause more bugs than they fix in my experience. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:
  • if(x) {}
  • foo();
  • }
  • And if I don't notice that extra } and continue to type:
  • if(x) {}
  • if(y) {
  • foo();
  • }
  • Not quite what I intended.
#1: Initial revision by user avatar Lundin‭ · 2020-10-30T16:10:34Z (about 4 years ago)
Yes, not using braces is considered bad practice by widely recognized industry coding standards (MISRA-C:2012 rule, 15.6, [CERT C EXP19-C](https://www.securecoding.cert.org/confluence/x/1QGMAg) and others).

Once upon a time I liked to skip out braces too, but every C (or other language) programmer using that style will ultimately write missing brace/indention goof-up bugs. Someone mentioned the "Apple goto fail" bug as one example, which I think is the single-most expensive software bug ever written. I used to write such bugs too now and then, until I abandoned that style. Particularly in long and complex `else-if` chains and such.

The CERT-C link above gives an excellent example:

    if (invalid_login())
      if (allow_guests())
        privileges = GUEST;
    else
      privileges = ADMINISTRATOR;

The else here ties to the inner if statement. Oops.

---

It is true that auto-indenting, auto-completion IDEs should reduce the chance for such bugs, but in my experience such IDEs are not nearly as smart as they could be - they cause more bugs than they fix in my experience. For example if I delete a { in the start of a statement in existing code, then type it again (because I replaced the whole if condition), the stupid auto-complete IDE will give you this:

    if(x) {}
      foo();
    }

And if I don't notice that extra } and continue to type:


    if(x) {}
      if(y) {
        foo(); 
    }

Not quite what I intended.