E
emma middlebrook
I have a question regarding asserting ... here's some code:
string GetAssertMessage()
{
... prepare a message string and return it...
}
void SomeMethod()
{
...
Debug.Assert(condition, GetAssertMessage());
...
}
Even if condition is true, GetAssertMessage() will still be evaluated
as it's a parameter to a function. How can I best avoid the cost of
GetAssertMessage() if the condition is false and not in DEBUG mode? I
also read that I shouldn't really put function calls into an Assert
statement (MSDN "Assertions in Managed Code").
Idea 1:
#ifdef DEBUG
string GetAssertMessage()
{
... prepare a message string ...
}
#endif
void SomeMethod()
{
...
#ifdef DEBUG
if (!condition)
{
string message = GetAssertMessage();
// could use Debug.Fail without condition
Debug.Assert(condition, GetAssertMessage());
}
#endif
}
Surely there must be something a little more elegant than this? It
just makes the code ugly.
Any ideas or have I done the best I can here?
Thanks!
Emma Middlebrook
(e-mail address removed)
string GetAssertMessage()
{
... prepare a message string and return it...
}
void SomeMethod()
{
...
Debug.Assert(condition, GetAssertMessage());
...
}
Even if condition is true, GetAssertMessage() will still be evaluated
as it's a parameter to a function. How can I best avoid the cost of
GetAssertMessage() if the condition is false and not in DEBUG mode? I
also read that I shouldn't really put function calls into an Assert
statement (MSDN "Assertions in Managed Code").
Idea 1:
#ifdef DEBUG
string GetAssertMessage()
{
... prepare a message string ...
}
#endif
void SomeMethod()
{
...
#ifdef DEBUG
if (!condition)
{
string message = GetAssertMessage();
// could use Debug.Fail without condition
Debug.Assert(condition, GetAssertMessage());
}
#endif
}
Surely there must be something a little more elegant than this? It
just makes the code ugly.
Any ideas or have I done the best I can here?
Thanks!
Emma Middlebrook
(e-mail address removed)