Why is short-circuiting not the default behavior in VB?

前端 未结 3 725
旧时难觅i
旧时难觅i 2021-01-17 10:25

VB has operators AndAlso and OrElse, that perform short-circuiting logical conjunction.

Why is this not the default behavior of And

3条回答
  •  无人共我
    2021-01-17 10:36

    I do not find short-circuiting to be useful in every case. I use it only when required. For instance, when checking two different and unconnected variables, it would not be required:

      If x > y And y > z Then
    
      End If
    

    As the article by Paul Vick illustrates (see link provided by Ken Browning above), the perfect scenario in which short-circuiting is useful is when an object has be checked for existence first and then one of its properties is to be evaluated.

      If x IsNot Nothing AndAlso x.Someproperty > 0 Then
    
      End If
    

    So, in my opinion both syntactical options are very much required.

提交回复
热议问题