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

后端 未结 19 2691
爱一瞬间的悲伤
爱一瞬间的悲伤 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 19:57

    Dissenting opinion (kind of)

    From a compilation standpoint, you're going to get the same IL, so it really only matters from a readability standpoint.

    From that standpoint, the if(value == false) is more obvious to a casual reader, and there is less chance of missing the ! before the bool.

    Honestly, I use both approaches, and most times, I make it depend on my variable name. If it still sounds ok to say "not" in place of the "bang", I'm likely to use the bang notation

    e.g.

    if(!gotValue) {}
    //if (I've) not gotValue
    
    //but
    
    if(checkValue == false){}
    //If (I've) not checkValue doesn't quite work here grammatically.
    

提交回复
热议问题