Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Assert' statements in .NET production code

Tags:

.net

assert

Is it wise to leave Trace.Assert and Debug.Assert statements in code that is "stable" and which has been moved into Test and Production environments?

If so, how do these assertion statements help? Isn't it sufficient to have Guard classes, etc. check for exception conditions and raise exceptions as appropriate?

like image 907
Kye Avatar asked Dec 07 '22 05:12

Kye


1 Answers

Debug.Assert statements will be ignored unless you have the DEBUG compilation constant defined, which by default happens when you compile in the "debug" configuration and not in the "release" configuration. Indeed, the Debug class is inteded to be used only in testing environments, where you are supposed to catch all (or at least most) of the bugs that would cause Debug.Assert to fail.

Trace.Assert works the same way, except that the compilation constant that must exist is TRACE, which by default exists in both "debug" and "release" configurations. It may make sense to have trace assertions in release code, but it is usually preferable to make them do something else than the default behavior of the method (which just displays a message box with the stack trace). You can achieve this by configuring a custom trace listener for the Trace class; see the method documentation for more details.

like image 193
Konamiman Avatar answered Jan 05 '23 08:01

Konamiman