Do Not Use the System Diagnostics Assert Methods Unless…
Assertions should never appear in release code. Assertions are a debug mechanism for revealing bugs within code during development. Failed assertions in release code indicate the bug was missed and allow the option of debugging at the assertion location. However, end users should not be presented with dialogs for debugging an application. Therefore, _
should be treated as obsolete._
One advantage of
will never appear in a release builds. In other words, the statement
is that the C# and VB compilers (not C++) eliminate the `
statement from the CIL code when the `
precompile constant is not defined. Therefore, even the conditional check in `
System.Diagnostics.Debug.Assert( ReallyLongRunningMethodThatReturnsBool() );
will not even call
(the C++ CLR compiler does not).
is defined. Therefore, use `
for expensive conditionals and replace the code in release builds with tests that explicitly check that the false conditional didn't occur. `
is useful for when the condition specified is a long running/expensive method and the compiler is C#, Visual Basic.NET, or some compiler that pays attention to the `
Another advantage of the assert is that it stops execution at the exact location of the assert, rather than bubbling up to the exception handler or as an unhandled exception. However, we still don’t want the assertion situation (the bad situation) to be ignored in release code, which is what would happen with
. Therefore, if the conditional is false, it should be reported as an exception not an assertion in release code.
Don’t use assertions unless:
- The assertion conditional is expensive and you have unit tests in place that check the false assertion condition doesn’t occur or
- You plan on a search and replace for them before releasing.
Otherwise, give up on assertion methods and use exceptions in both release and debug builds.