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
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...
Answer
#4: Post edited
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
- 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
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
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.