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

前端 未结 3 726
旧时难觅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:28

    Because the VB team had to maintain backward-compatibility with older code (and programmers!)

    If short-circuiting was the default behavior, bitwise operations would get incorrectly interpreted by the compiler.

    The Ballad of AndAlso and OrElse by Panopticon Central

    Our first thought was that logical operations are much more common than bitwise operations, so we should make And and Or be logical operators and add new bitwise operators named BitAnd, BitOr, BitXor and BitNot (the last two being for completeness). However, during one of the betas it became obvious that this was a pretty bad idea. A VB user who forgets that the new operators exist and uses And when he means BitAnd and Or when he means BitOr would get code that compiles but produces "bad" results.

提交回复
热议问题