将布尔函数放在 if 语句表达式中

发布于 2024-09-08 21:09:38 字数 473 浏览 4 评论 0原文

我正在维护一个 VB6 应用程序。对于布尔函数,原作者在检查 If 语句中的结果之前将返回值存储在布尔变量中。我更喜欢将函数直接放在 If 语句中。这是一个偏好问题还是我错过了我的风格中的潜在陷阱?

原创作者风格

bReturn = IsThisFun(maintainingVb6)
If bReturn = True Then
    'You haven't used .NET
Else
    'blah
End If

我的风格

If IsThisFun(maintainingVb6) Then
    'You haven't used .NET
Else
    'blah
End If

我不确定这些不同的方法是否有合适的术语,这可能让我错过了之前关于这个主题的帖子。

谢谢

I'm maintaining a VB6 app. For boolean functions, the original authors store the return value in a boolean variable before checking the result in an If Statement. I prefer to place the function directly in the If Statement. Is this a matter of preference or am I missing a potential pitfall in my style?

Original Author Style

bReturn = IsThisFun(maintainingVb6)
If bReturn = True Then
    'You haven't used .NET
Else
    'blah
End If

My Style

If IsThisFun(maintainingVb6) Then
    'You haven't used .NET
Else
    'blah
End If

I'm not sure if there is a proper terminology for these different approaches which may have allowed me to miss a previous post on this topic.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

十雾 2024-09-15 21:09:38

做最具可读性和可维护性的事情。在我看来,你的风格 98% 的情况下都符合这个标准。

Do whatever is most readable and maintainable. In my opinion, your style meets that criteria 98% of the time.

葵雨 2024-09-15 21:09:38

我相信原作者创建了一个变量来调试应用程序。他会在第 2 行放置一个断点,并将鼠标悬停在 bReturn 上来查看它的值。但由于您还可以将鼠标悬停在等号上或在 If/EndIf 语句内放置两个断点,因此无需为此创建变量。

顺便说一句,我部分同意 DOK 的观点。无需创建不必要的变量。这根本不是浪费资源,因为编译器足够聪明,可以删除这个变量,但是创建这样的变量并不会使代码更具可读性。

I believe the original author created a variable to be able to debug the application. He would put a breakpoint at line 2, and will see the value of bReturn by hovering on it. But since you can also hover on equal symbol or put two breakpoints inside If/EndIf statement, there is no need to create a variable just for this.

By the way, I partially agree with DOK. There is no need to create unnecessary variables. It's not at all a waste of resources, because compiler is clever enough to remove this variable, but creating a variable such this does not make a code more readable.

妖妓 2024-09-15 21:09:38

严格来说,这是风格问题。

经验法则是:什么更具可读性。您的代码可能是首选,因为他的代码是冗余的,并且大多数人更喜欢删除冗余。 (他至少可以删除 = True,这完全是多余的。)

如果布尔值是较长函数调用的结果,则 If then 正在变得顺便说一句,我会将结果作为一个单独的变量,以便更容易地查看函数调用,但这只是我。

Strictly speaking, it's a matter of style.

The rule of thumb is: what's more readable. Yours is probably to be preferred, because his code is redundant and most people prefer to remove redundancy. (The least he could do is remove the = True, which is totally redundant.)

If the boolean was a result of a longer function call, where the If Then was getting in the way, I would make the result a separate variable to make seeing the function call easier, but that's just me.

新人笑 2024-09-15 21:09:38

使用任一方法(w/ 或 wo/ 变量)都可能存在一个陷阱。您必须确保该函数返回实际的 VB 布尔值(OLE VARIANTBOOL),而不是 C 布尔值或任何其他类型。如果函数是用全 VB6 编写和公开的,这不是问题,但是...

使用 As Boolean< 为原型为返回 bool 的外部(通常是 API)函数创建声明并不罕见。 /code> 返回类型。 VB 接受这一点,但当返回“True”(来自函数的 POV)时,它可能处于异常状态:包含值 1 的 VB 16 位布尔值,而不是正常的 -1。

这可能会产生奇怪的后续效果,最明显的是对 If Not MyFunction() 的测试,它将计算 (Not 1) 或 -2,而不是正如预期的那样,(不是 -1) 或 0。因此,您会得到 Not(True) = True 的奇怪结果。

最佳实践是声明外部“bool”函数As LongCBool​​() 结果。

There's one possible pitfall in using either method (w/ or wo/ variable). You have to certain that the function returns an actual VB Boolean (an OLE VARIANTBOOL), not a C bool or any other type. If the function is written and exposed in all-VB6, this isn't an issue, but...

It's not uncommon to create Declares for external (usually API) functions that are prototyped as returning bool, using As Boolean for the return type. VB accepts that, but when a 'True' (from the function's POV) is returned it may be in an anomalous state: a VB 16-bit Boolean that contains the value 1, instead of the normal -1.

This can have strange follow-on effects, the most obvious being a test for If Not MyFunction(), which will compute (Not 1), or -2, instead of (Not -1), or 0, as expected. So you get the strange result of Not(True) = True

The best practice is to Declare external 'bool' functions As Long and CBool() the result.

拥抱没勇气 2024-09-15 21:09:38

我不喜欢声明不必要的变量。
这是浪费资源,并且不会使代码更具可读性。
我更喜欢你的风格。

I don't ever like declaring unnecessary variables.
It's a waste of resources, and doesn't make the code more readable.
I much prefer your style.

悍妇囚夫 2024-09-15 21:09:38

实际上,这通常不是偏好问题。如果代码包装在 On Error Resume Next 中,则单独的本地变量很重要。

如果 IsThisFun 失败,则 bReturn 不会被分配,并且通常最终为 false。所以条件不满足。在第二个片段中,如果 IsThisFun 引发异常,则执行将进入 If true 块。

为了克服这个问题,我看到了许多“无意义”的片段,如下所示:

On Error Resume Next
...
If Not IsThisFun(maintainingVb6) Then
Else
    ...
End If

Actually most often than not this is not a matter of preferences. The separate local var matters if the code is wrapped in On Error Resume Next.

If IsThisFun bombs out then bReturn does not get assigned and usually ends up being false. so the condition is not satisfied. In the second snippet if IsThisFun raises an exception then the execution gets inside the If true block.

For overcoming this I've seen numerous "non-sense" snippets like this:

On Error Resume Next
...
If Not IsThisFun(maintainingVb6) Then
Else
    ...
End If
想挽留 2024-09-15 21:09:38

难道你不能创建一个返回 bool 类型的方法吗?

例子:

public bool maintainingVB6()
{
  return true;
}

Can't you just make a method that returns a type of bool instead?

Example:

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