Which is clearer form: if(!value) or if(flag == value)?

后端 未结 19 2696
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 19:07

I understand this is a subjective question, so I apologize if it needs to be closed, but I feel like it comes up often enough for me to wonder if there is a general preferen

19条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 20:00

    if (!value) is easier/faster to follow. Subjective as you said. As long as you are consistent, this is the main thing.

    EDIT

    One other point to add - omitting the true/false keywords should also (hopefully) force the coder to use better named variables. Bool variables should always indicate meaning or state purpose, such as:

    if (MyWallet.IsEmpty)

    There is no reason with the above to use == false or == true as it's redundant. The above is human readable immediately.

    Far better than having to decipher:

    if (MyWallet.EmptyStatus == true) or something ridiculous like this.

提交回复
热议问题