Why is order of expressions in if statement important

后端 未结 9 2092
无人共我
无人共我 2021-01-05 03:01

Suppose I have an IF condition :

if (A || B) 
    ∧
    |
    |
   left
{ 
   // do something  
}

Now suppose that A

9条回答
  •  清歌不尽
    2021-01-05 03:54

    In many cases there is no practical difference apart from a tiny performance improvement. Where this becomes useful is if your checks are very expensive function calls (unlikely) or you need to check things in order. Say for example you want to check a property on something and to check if that something is nil first, you might do something like:

    If (a != nil && a.attribute == valid) {}

提交回复
热议问题