Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-11790

C and C++ Style Guide Proposal: Use Braces for Multi-line if/for/while Statements

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      Introduction

      Wiredtiger's current C Style guide doesn't require curly braces for multiline if/for/while statements:

      Single statement blocks in conditions and loops do not use braces unless required to avoid ambiguity.

      Proposed style change

      • If an if, while, or for statement spans more than one line, the body must be enclosed in braces.
      • For single-line statements the preferred style is not to use braces but they can be used if it improves readability or expressiveness.

      Exceptions

      • If for any reason braces can't be used, there must be a comment explaining it.

      Rationale

      • Helps prevent common mistakes like the dangling else problem.
      • Enhances code clarity by making block boundaries explicit.
      • Makes code easier to maintain: less chance to introduce an error by adding or removing an adjacent line.
      • With auto-formatting, an introduced error can be harder to spot when there are no braces.

      Examples

      Simple statement

      Instead of

          if (condition)
              myFunction();
      

      use

          if (condition) myFunction();
      

      or

          if (condition) {
              myFunction();
          }
      

      Multiline comment

      Instead of

          if (condition)
              /*
               * Comment
               */
              myFunction();
      

      use

          if (condition) {
              /*
               * Comment
               */
              myFunction();
          }
      

      If-Else

      Instead of

          if (condition)
              myFunction();
          else
              myFunction();
      

      use

          if (condition) {
              myFunction();
          } else {
              myFunction();
          }
      

      Nested statements

      Instead of

          if (condition1)
              if (condition2)
                  myFunction1();
            else  /* What is this else even for? */
                myFunction2();
          for (init; condition1; incr)
              if (condition2)
                  myFunction1();
          if (condition1)
              while (condition2)
                  myFunction2();
      

      use

          if (condition1) {
              if (condition2) {
                  myFunction1();
              }
          } else {
              myFunction2();
          }
          for (init; condition1; incr) {
              if (condition2) {
                  myFunction1();
              }
          }
          if (condition1) {
              while (condition2) {
                  myFunction2();
              }
          }
      

            Assignee:
            backlog-server-storage-engines [DO NOT USE] Backlog - Storage Engines Team
            Reporter:
            y.ershov@mongodb.com Yury Ershov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: