1

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?

serhio
  • 28,010
  • 62
  • 221
  • 374

5 Answers5

3

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 :)

taylonr
  • 10,732
  • 5
  • 37
  • 66
  • glad to hear that this is confusing even for native anglophones ... :-) – serhio Apr 19 '11 at 10:30
  • +1. The same backwardness happens when you try to write messages for the various Assert methods in your favorite unit testing framework. That problem is sometimes made worse by the fact that you have a variety of Asserts with minor differences in semantics. – Christoffer Lette Apr 19 '11 at 10:48
  • @Christoffer, we solved that by using the Should plugin in .Net, it changes Asserts.Equals(1, myVal) to myVal.ShouldEqual(1) – taylonr Apr 19 '11 at 11:22
  • Sounds cool. But I can't find it. Maybe my Google Fu is lacking. Are you possibly referring to [SpecUnit.Net](http://code.google.com/p/specunit-net/)? – Christoffer Lette Apr 19 '11 at 14:01
  • http://should.codeplex.com/ for .net and http://eliperelman.com/jshould for javascript – taylonr Apr 19 '11 at 14:26
1

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.

Steven
  • 166,672
  • 24
  • 332
  • 435
0

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.

Paulo Santos
  • 11,285
  • 4
  • 39
  • 65
0

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

Frederik Slijkerman
  • 6,471
  • 28
  • 39
  • I understand what it does, I just don't understand the "counter-intuitive" behavior ) – serhio Apr 19 '11 at 10:32
  • 1
    Intuitively we would say "when i != 9 then something's wrong", but this doesn't translate to `assert(i != 9, "something's wrong");` and that's why `assert` is a bit counter-intuitive. – Steven Apr 19 '11 at 10:33
  • Personally I never add strings to assertions, the file name and line number is good enough to track them down should they fail. – Frederik Slijkerman Apr 19 '11 at 10:34
0

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#.

Polyfun
  • 9,479
  • 4
  • 31
  • 39