简体   繁体   English

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

[英]Debug Assert condition logic and language peculiarities

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. 我不是母语为英语的人,所以对我来说, Debug.Assert方法有点奇怪,对我来说,验证一个条件,实际上验证了这个条件的反面。

Assert = advance, affirm, argue, attest, aver, claim, proclaim, profess, pronounce, put forward, say, stress, etc 断言 =推进,肯定,争辩,证明,aver,声称,宣告,宣传,发音,提出,说,压力等

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? 你看到这个直观与否?

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. 这将断言Me.Member is Nothing ,并且只会在不是时提醒您。 Kind of like if you were in an argument and you said "The Earth is 75% water." 有点像你是在争论,你说“地球是75%的水”。 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. 对于Assert也是如此,如果断言为真,则不需要调试器说出来。 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 :) 顺便说一句,我是一个以英语为母语的人,前几次我写了断言语,我向后写了:)

The API for Debug.Assert is indeed a bit counter-intuitive. Debug.Assert的API确实有点违反直觉。 It took me a long time to get used to this. 我花了很长时间才习惯这个。 In my head the Assert translated to this: 在我的脑海里, Assert翻译成这个:

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. 诀窍是布尔表达式应为true 而不是断言。 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. 不过我认为这个设计已经被选中了,因为这个API已经为C ++开发人员提供了很长时间,并且交换它会让他们眼花缭乱。

The point is that your assumption of the Assert method is a bit off the mark. 关键是你对Assert方法的假设有点偏僻。

I mean, the function checks if the condition is valid, that is, TRUE. 我的意思是,该函数检查条件是否有效,即TRUE。 So it ASSERTS that the condition. 所以它肯定了条件。

IF the condition is not met, that is, FALSE, the message is displayed. 如果不满足条件,即FALSE,则显示消息。

With Assert , you verify the logic in your code. 使用Assert ,可以验证代码中的逻辑。 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.) (抱歉,C ++代码。)

You are not missing anything. 你没有遗漏任何东西。 Perhaps the designers should have called the method "IfNot" instead of "Assert". 也许设计师应该调用方法“IfNot”而不是“Assert”。 I suppose they did not mean an Assert call to be read like an "if" statement. 我想他们并不是说Assert调用被读作“if”语句。 "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#. “断言”作为检查值的方法名称在C / C ++中也有很长的历史,所以我猜他们想在C#中使用相同的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM