为什么短路不是 VB 中的默认行为?

发布于 2024-07-12 10:44:33 字数 424 浏览 6 评论 0原文

VB 具有运算符 AndAlsoOrElse,执行短路逻辑连词。

为什么这不是 AndOr 表达式的默认行为,因为短路在每种情况下都很有用。

奇怪的是,这与大多数语言相反&&||执行短路。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

時窥 2024-07-19 10:44:34

显式短路可确保首先评估左侧操作数。

在 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).

月下凄凉 2024-07-19 10:44:33

因为 VB 团队必须保持与旧代码(以及程序员!)的向后兼容性

,如果短路是默认行为,则编译器将错误地解释按位操作。

AndAlso 和 OrElse 之歌 圆形监狱中央

我们的第一个想法是逻辑运算比按位运算更常见,因此我们应该将 And 和 Or 设为逻辑运算符,并添加新的按位运算符,名为 BitAnd、BitOr、BitXor 和 BitNot(最后两个是为了完整性)。 然而,在其中一个测试期间,很明显这是一个非常糟糕的主意。 VB 用户忘记了新运算符的存在并使用 And(当他表示 BitAnd 时)和 Or(当他表示 BitOr 时)将得到编译但产生“不良”结果的代码。

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.

单身狗的梦 2024-07-19 10:44:33

我不认为短路在所有情况下都有用。 我只在需要时使用它。 例如,当检查两个不同且未连接的变量时,就不需要这样做:

  If x > y And y > z Then

  End If

正如 Paul Vick 的文章所示(请参阅上面 Ken Browning 提供的链接),短路有用的完美场景是当一个对象已被首先检查其存在性,然后评估其属性之一。

  If x IsNot Nothing AndAlso x.Someproperty > 0 Then

  End If

因此,在我看来,这两种语法选项都是非常需要的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文