Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it worth using Debug.Assert in ASP.NET?

It seems like a fairly large hassle to set up a proper debug environment in ASP.NET and I'm just wondering if using Asserts are the way to go or not. I've read a bit and saw that you need to modify your web.config to properly use Asserts. Is this usually the best way to go or are there other methods of debugging that might be easier to use?

We don't use a unit testing framework so that isn't really relevant to the question.

How do you know the difference between them working properly or not working at all? Currently I can put in asserts in my code and it will do absolutely nothing because they are not configured in the web.config. This seems dangerous to me.

like image 253
Joe Phillips Avatar asked Apr 17 '09 21:04

Joe Phillips


People also ask

What is the use of debug assert?

Assert(Boolean, Debug+AssertInterpolatedStringHandler) Checks for a condition; if the condition is false , outputs a specified message and displays a message box that shows the call stack.

Can asserts help with debugging?

Assertions are a convenient tool for documenting, debugging, and testing code during development. Once you've debugged and tested your code with the help of assertions, then you can turn them off to optimize the code for production. Assertions will help you make your code more efficient, robust, and reliable.

When should assert be used?

Use assert when you know some condition must prevail in order for the code to be considered "good." If the assert fails, then by definition the code must be fixed. @Robert: agreed +1, but there must be consideration given to how much work the user will lose if the assertion firing crashes the program.

How do the assertions help with the debugging?

Assertions provide a useful debugging tool that allows you to document an assumption within your code and have this assumption checked automatically at run-time. Every assertion includes a predicate, or Boolean condition, that you expect will always evaluate as true.


2 Answers

I would direct you here: When should I use Debug.Assert()?. There are several good answers that can tell you when it's good to use them, and you can figure out from there if it's worth it in your app.

like image 182
Tom Ritter Avatar answered Sep 20 '22 13:09

Tom Ritter


Having Debug Asserts will ensure your code is correct. With the right combination of test cases will definitely help you.

Several Unit test frameworks come with handlers that can log messages and throw exceptions on asserts. Choosing one of these framework or writing your own handler is something that you may have to think about. But once the Unit test code catches these exceptions, they should be logged and marked as failed.

like image 40
Srikar Doddi Avatar answered Sep 18 '22 13:09

Srikar Doddi