What's the purpose of using braces (i.e. {}) for a single-line if or loop?

前端 未结 23 1388
独厮守ぢ
独厮守ぢ 2020-11-28 00:33

I\'m reading some lecture notes of my C++ lecturer and he wrote the following:

  1. Use Indentation // OK
  2. Never rely on operator preced
23条回答
  •  眼角桃花
    2020-11-28 01:30

    My 2c:

    Use Indentation

    Obviously

    Never rely on operator precedence - Always use parentheses

    I wouldn't use words "never and "always", but in general I see this rule being useful. In some languages (Lisp, Smalltalk) this is a non-issue.

    Always use a { } block - even for a single line

    I never do that and never had a single problem, but I can see how it can be good for students, esp. if they studied Python before.

    Const object on left side of comparison

    Yoda conditions? No, please. It hurts readability. Just use the maximum warning level when you compile your code.

    Use unsigned for variables that are >= 0

    OK. Funny enough, I've heard Stroustrup disagree.

    Set Pointer to NULL after deletion - Double delete protection

    Bad advice! Never have a pointer which points to a deleted or non-existing object.

提交回复
热议问题