调试断言条件逻辑和语言特性

发布于 2024-11-02 09:09:41 字数 572 浏览 4 评论 0原文

我的母语不是英语,所以对我来说 Debug.Assert 方法有点奇怪,对我来说,它验证一个条件,实际上验证与此条件相反的条件。

断言 = 提前、确认、 争论、证明、主张、主张、 宣告、宣称、宣布、提出、说、 压力等

我期望,表现

Debug.Assert(Me.Member Is Nothing, "Member Is Nothing!")

得像

Affirm(condition, "Message") ' conditional message

但是,实际上我应该做

Debug.Assert(Me.Member Is Nothing, "Member Is NOT Nothing!")

Affirm(NOT condition, "Message") ' counter-conditional message

我错过了什么吗?

你是否直观地看到了这一点?

I am not native English speaker, so for me is a little strange the Debug.Assert method, that, for me, verifies a condition, in fact, verifies the opposite of this condition.

Assert = advance, affirm,
argue, attest, aver, claim,
proclaim, profess, pronounce, put forward, say,
stress, etc

I expect, that the

Debug.Assert(Me.Member Is Nothing, "Member Is Nothing!")

behave like

Affirm(condition, "Message") ' conditional message

However, in reality I should do

Debug.Assert(Me.Member Is Nothing, "Member Is NOT Nothing!")

Affirm(NOT condition, "Message") ' counter-conditional message

Do I miss something?

Do you see this intuitive or not?

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

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

发布评论

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

评论(5

怪我闹别瞎闹 2024-11-09 09:09:41

您的语法是正确的,但请这样想。

Debug.Assert(Me.Member Is Nothing, "Member assert.")

这将断言 Me.Member is Nothing,并且只会在事实并非如此时提醒您。
有点像如果你在争论中说“地球的 75% 是水”。没有太多人会在这一点上与你争论。但如果你说“地球主要是由巧克力蛋糕组成的”。人们会争论。

对于断言也是如此,如果断言为真,则调试器不需要说话。如果它是假的,它需要让你知道。

顺便说一句,我的母语是英语,前几次我写断言语句时,我是倒着写的:)

You have the syntax correct, but think of it this way

Debug.Assert(Me.Member Is Nothing, "Member assert.")

This will assert that Me.Member is Nothing, and will only alert you when it's not.
Kind of like if you were in an argument and you said "The Earth is 75% water." Not too many people are going to argue with you on that. But if you said "The Earth is mostly made up of chocolate cake." People would argue.

So too with Assert, if the assertion is true, no need for the debugger to speak up. If it's false, it needs to let you know.

By the way, I'm a native English speaker, and the first few times I wrote assert statements, I wrote them backwards :)

高跟鞋的旋律 2024-11-09 09:09:41

Debug.Assert 的 API 确实有点违反直觉。我花了很长时间才习惯这一点。在我的脑海中,Assert 翻译为:

if (condition)
{
    Fail(message);
}

但事实上,它翻译为:

if (condition)
{
    // Success
}
else
{
    Fail(message);
}

或简而言之:

if (!condition)
{
    Fail(message);
}

诀窍是布尔表达式应该为 true,而不 才能断言。我认为无论如何选择这个设计,是因为这个 API 已经为 C++ 开发人员提供了很长一段时间,并且交换它会让他们眼花缭乱。

The API for Debug.Assert is indeed a bit counter-intuitive. It took me a long time to get used to this. In my head the Assert translated to this:

if (condition)
{
    Fail(message);
}

But in fact, it translated to:

if (condition)
{
    // Success
}
else
{
    Fail(message);
}

or in short:

if (!condition)
{
    Fail(message);
}

The trick is that the boolean expression should be true not to assert. I think however this design was picked, because this API has been available for C++ developers for a long time and swapping it around would dazzle them.

兔姬 2024-11-09 09:09:41

关键是您对 Assert 方法的假设有点偏离目标。

我的意思是,该函数检查条件是否有效,即 TRUE。所以它断言条件。

如果不满足条件,即 FALSE,则显示该消息。

The point is that your assumption of the Assert method is a bit off the mark.

I mean, the function checks if the condition is valid, that is, TRUE. So it ASSERTS that the condition.

IF the condition is not met, that is, FALSE, the message is displayed.

半仙 2024-11-09 09:09:41

使用 Assert,您可以验证代码中的逻辑。或者,您可以添加一条消息,以帮助在断言失败时进行调试。

int i = 3;
i *= 3;
assert(i == 9, "Strange arithmetic failure");

(抱歉,C++ 代码。)

With Assert, you verify the logic in your code. Optionally, you can add a message to help with debugging should an assertion fail.

int i = 3;
i *= 3;
assert(i == 9, "Strange arithmetic failure");

(Sorry for the C++ code.)

甜是你 2024-11-09 09:09:41

你没有错过任何东西。也许设计者应该将该方法称为“IfNot”而不是“Assert”。我想他们并不意味着将 Assert 调用理解为“if”语句。 “Assert”作为检查值的方法名称在 C/C++ 中也有很长的历史,所以我猜他们想在 C# 中使用相同的名称。

You are not missing anything. Perhaps the designers should have called the method "IfNot" instead of "Assert". I suppose they did not mean an Assert call to be read like an "if" statement. "Assert" as a method name for checking values also has a long history in C/C++, so I guess they wanted to use the same name in C#.

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