为什么短路不是 VB 中的默认行为?
VB 具有运算符 AndAlso 和 OrElse,执行短路逻辑连词。
为什么这不是 And 和 Or 表达式的默认行为,因为短路在每种情况下都很有用。
奇怪的是,这与大多数语言相反,&&和||执行短路。
VB has operators AndAlso and OrElse, that perform short-circuiting logical conjunction.
Why is this not the default behavior of And and Or expressions since short-circuiting is useful in every case.
Strangely, this is contrary to most languages where && and || perform short-circuiting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显式短路可确保首先评估左侧操作数。
在 VB 以外的某些语言中,逻辑运算符可以执行隐式短路,但可以首先计算右侧运算符(例如取决于逻辑运算符左侧和右侧表达式的复杂性)。
Explicit short-circuit makes sure that the left operand is evaluated first.
In some languages other than VB, logical operators may perform an implicit short circuit but may evaluate the right operator first (depending for instance on the complexity of the expressions at left and at right of the logical operator).
因为 VB 团队必须保持与旧代码(以及程序员!)的向后兼容性
,如果短路是默认行为,则编译器将错误地解释按位操作。
AndAlso 和 OrElse 之歌 圆形监狱中央
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
我不认为短路在所有情况下都有用。 我只在需要时使用它。 例如,当检查两个不同且未连接的变量时,就不需要这样做:
正如 Paul Vick 的文章所示(请参阅上面 Ken Browning 提供的链接),短路有用的完美场景是当一个对象已被首先检查其存在性,然后评估其属性之一。
因此,在我看来,这两种语法选项都是非常需要的。
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:
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.
So, in my opinion both syntactical options are very much required.