Boolean checking in the 'if' condition

后端 未结 10 1985
陌清茗
陌清茗 2020-12-03 07:40

Which one is better Java coding style?

boolean status = true;
if (!status) {
    //do sth
} else {
    //do sth
}

or:

10条回答
  •  伪装坚强ぢ
    2020-12-03 08:21

    It really also depends on how you name your variable.

    When people are asking "which is better practice" - this implicitly implies that both are correct, so it's just a matter of which is easier to read and maintain.

    If you name your variable "status" (which is the case in your example code), I would much prefer to see

    if(status == false) // if status is false

    On the other hand, if you had named your variable isXXX (e.g. isReadableCode), then the former is more readable. consider:

    if(!isReadable) { // if not readable
      System.out.println("I'm having a headache reading your code");
    }
    

提交回复
热议问题