Mandatory use of braces

前端 未结 21 2179
旧时难觅i
旧时难觅i 2021-02-12 23:26

As part of a code standards document I wrote awhile back, I enforce \"you must always use braces for loops and/or conditional code blocks, even (especially) if they\'re only one

相关标签:
21条回答
  • 2021-02-13 00:12

    I prefer adding braces to single-line conditionals for maintainability, but I can see how doing it without braces looks cleaner. It doesn't bother me, but some people could be turned off by the extra visual noise.

    I can't offer a better counterargument either. Sorry! ;)

    0 讨论(0)
  • 2021-02-13 00:14

    I find it hard to argue with coding standards that reduce errors and make the code more readable. It may feel ugly to some people at first, but I think it's a perfectly valid rule to implement.

    0 讨论(0)
  • 2021-02-13 00:15

    I think the important thing about braces is that they very definitely express the intent of the programmer. You should not have to infer intent from indentation.

    That said, I like the single-line returns and continues suggested by Gus. The intent is obvious, and it is cleaner and easier to read.

    0 讨论(0)
  • 2021-02-13 00:16

    The only way coding standards can be followed well by a group of programmers is to keep the number of rules to a minimum.

    Balance the benefit against the cost (every extra rule confounds and confuses programmers, and after a certain threshold, actually reduces the chance that programmers will follow any of the rules)

    So, to make a coding standard:

    • Make sure you can justify every rule with clear evidence that it is better than the alternatives.

    • Look at alternatives to the rule - is it really needed? If all your programmers use whitespace (blank lines and indentation) well, an if statement is very easy to read, and there is no way that even a novice programmer can mistake a statement inside an "if" for a statement that is standalone. If you are getting lots of bugs relating to if-scoping, the root cause is probably that you have a poor whitepsace/indentation style that makes code unnecessarily difficult to read.

    • Prioritise your rules by their measurable effect on code quality. How many bugs can you avoid by enforcing a rule (e.g. "always check for null", "always validate parameters with an assert", "always write a unit test" versus "always add some braces even if they aren't needed"). The former rules will save you thousands of bugs a year. The brace rule might save you one. Maybe.

    • Keep the most effective rules, and discard the chaff. Chaff is, at a minimum, any rule that will cost you more to implement than any bugs that might occur by ignoring the rule. But probably if you have more than about 30 key rules, your programmers will ignore many of them, and your good intentions will be as dust.

    • Fire any programmer who comments out random bits of code without reading it :-)

    P.S. My stance on bracing is: If the "if" statement or the contents of it are both a single line, then you may omit the braces. That means that if you have an if containing a one-line comment and a single line of code, the contents take two lines, and therefore braces are required. If the if condition spans two lines (even if the contents are a single line), then you need braces. This means you only omit braces in trivial, simple, easily read cases where mistakes are never made in practice. (When a statement is empty, I don't use braces, but I always put a comment clearly stating that it is empty, and intentionally so. But that's bordering on a different topic: being explicit in code so that readers know that you meant a scope to be empty rather than the phone rang and you forgot to finish the code)

    0 讨论(0)
  • 2021-02-13 00:17

    Here are the unwritten (until now I suppose) rules I go by. I believe it provides readability without sacrificing correctness. It's based on a belief that the short form is in quite a few cases more readable than the long form.

    • Always use braces if any block of the if/else if/else statement has more than one line. Comments count, which means a comment anywhere in the conditional means all sections of the conditional get braces.
    • Optionally use braces when all blocks of the statement are exactly one line.
    • Never place the conditional statement on the same line as the condition. The line after the if statement is always conditionally executed.
    • If the conditional statement itself performs the necessary action, the form will be:

      for (init; term; totalCount++)
      {
          // Intentionally left blank
      }
      

    No need to standardize this in a verbose manner, when you can just say the following:

    Never leave braces out at the expense of readability. When in doubt, choose to use braces.

    0 讨论(0)
  • 2021-02-13 00:18

    At present, I work with a team that lives by this standard, and, while I'm resistant to it, I comply for uniformity's sake.

    I object for the same reason I object to teams that forbid use of exceptions or templates or macros: If you choose to use a language, use the whole language. If the braces are optional in C and C++ and Java, mandating them by convention shows some fear of the language itself.

    I understand the hazards described in other answers here, and I understand the yearning for uniformity, but I'm not sympathetic to language subsetting barring some strict technical reason, such as the only compiler for some environment not accommodating templates, or interaction with C code precluding broad use of exceptions.

    Much of my day consists of reviewing changes submitted by junior programmers, and the common problems that arise have nothing to do with brace placement or statements winding up in the wrong place. The hazard is overstated. I'd rather spend my time focusing on more material problems than looking for violations of what the compiler would happily accept.

    0 讨论(0)
提交回复
热议问题