When should I use Debug.Assert()?
FWIW ... I find that my public methods tend to use the if () { throw; }
pattern to ensure that the method is being called correctly. My private methods tend to use Debug.Assert()
.
The idea is that with my private methods, I'm the one under control, so if I start calling one of my own private methods with parameters that are incorrect, then I've broken my own assumption somewhere--I should have never gotten into that state. In production, these private asserts should ideally be unnecessary work since I am supposed to be keeping my internal state valid and consistent. Contrast with parameters given to public methods, which could be called by anyone at runtime: I still need to enforce parameter constraints there by throwing exceptions.
Additionally, my private methods can still throw exceptions if something doesn't work at runtime (network error, data access error, bad data retrieved from a third party service, etc.). My asserts are just there to make sure that I haven't broken my own internal assumptions about the state of the object.
From Code Complete
8 Defensive Programming
8.2 Assertions
An assertion is code that’s used during development—usually a routine or macro—that allows a program to check itself as it runs. When an assertion is true, that means everything is operating as expected. When it’s false, that means it has detected an unexpected error in the code. For example, if the system assumes that a customer-information file will never have more than 50,000 records, the program might contain an assertion that the number of records is lessthan or equal to 50,000. As long as the number of records is less than or equal to 50,000, the assertion will be silent. If it encounters more than 50,000 records, however, it will loudly “assert” that there is an error in the program.
Assertions are especially useful in large, complicated programs and in high reliability programs. They enable programmers to more quickly flush out mismatched interface assumptions, errors that creep in when code is modified, and so on.
An assertion usually takes two arguments: a boolean expression that describes the assumption that’s supposed to be true and a message to display if it isn’t.
(…)
Normally, you don’t want users to see assertion messages in production code; assertions are primarily for use during development and maintenance. Assertions are normally compiled into the code at development time and compiled out of the code for production. During development, assertions flush out contradictory assumptions, unexpected conditions, bad values passed to routines, and so on. During production, they are compiled out of the code so that the assertions don’t degrade system performance.
Put Debug.Assert()
everywhere in the code where you want have sanity checks to ensure invariants. When you compile a Release build (i.e., no DEBUG
compiler constant), the calls to Debug.Assert()
will be removed so they won't affect performance.
You should still throw exceptions before calling Debug.Assert()
. The assert just makes sure that everything is as expected while you're still developing.
In Debugging Microsoft .NET 2.0 Applications John Robbins has a big section on assertions. His main points are:
- Assert liberally. You can never have too many assertions.
- Assertions don't replace exceptions. Exceptions cover the things your code demands; assertions cover the things it assumes.
- A well-written assertion can tell you not just what happened and where (like an exception), but why.
- An exception message can often be cryptic, requiring you to work backwards through the code to recreate the context that caused the error. An assertion can preserve the program's state at the time the error occurred.
- Assertions double as documentation, telling other developers what implied assumptions your code depends on.
- The dialog that appears when an assertion fails lets you attach a debugger to the process, so you can poke around the stack as if you had put a breakpoint there.
PS: If you liked Code Complete, I recommend following it up with this book. I bought it to learn about using WinDBG and dump files, but the first half is packed with tips to help avoid bugs in the first place.