调试断言条件逻辑和语言特性
我的母语不是英语,所以对我来说 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的语法是正确的,但请这样想。
这将断言
Me.Member is Nothing
,并且只会在事实并非如此时提醒您。有点像如果你在争论中说“地球的 75% 是水”。没有太多人会在这一点上与你争论。但如果你说“地球主要是由巧克力蛋糕组成的”。人们会争论。
对于断言也是如此,如果断言为真,则调试器不需要说话。如果它是假的,它需要让你知道。
顺便说一句,我的母语是英语,前几次我写断言语句时,我是倒着写的:)
You have the syntax correct, but think of it this way
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 :)
Debug.Assert
的 API 确实有点违反直觉。我花了很长时间才习惯这一点。在我的脑海中,Assert
翻译为:但事实上,它翻译为:
或简而言之:
诀窍是布尔表达式应该为 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 theAssert
translated to this:But in fact, it translated to:
or in short:
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.
关键是您对
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.
使用
Assert
,您可以验证代码中的逻辑。或者,您可以添加一条消息,以帮助在断言失败时进行调试。(抱歉,C++ 代码。)
With
Assert
, you verify the logic in your code. Optionally, you can add a message to help with debugging should an assertion fail.(Sorry for the C++ code.)
你没有错过任何东西。也许设计者应该将该方法称为“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#.