When should I debug.assert over code contracts or vice versa? I want to check precondition for a method and I am confused to choose one over the other. I have unit tests where I want to test failure scenarios and expect exceptions.
Is it a good practice to use Debug.Assert and Code contract on the same method. If so what would be the order in which the code should be written?
Debug.Assert(parameter!= null); Contract.Requires<ArgumentNullException>(parameter != null, "parameter");
or
Contract.Requires<ArgumentNullException>(parameter != null, "parameter"); Debug.Assert(parameter!= null);
Is there any rationale behind it?
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.
What is an Assertion? 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.
A: Contract assertions give the programmer a way to deliver precise facts to the toolchain about the expected state of a program at one point.
Assert() statements (as well as other Debug class method invocations) are ignored in a Release build.
These are different things. A debug assert is only executed when the code is compiled as debug and therefore will only check/assert under debug. The idea is to use this for "sanity checks" for code you are developing. Code contracts can be used in either debug or release. They assure that pre and post conditions of methods comply with the expectations of the method (meet the contract). There is also a testing framework that provides similar functionality, designed for checking test compliance.
Use Debug.Assert when you want ensure that certain things are as you expect when developing the code (and in later maintenance development).
Use code contracts when you want to assure that conditions are true in both debug and release. Contracts also allow certain forms of static analysis that can be helpful in verifying that your program is "correct".
Use the Testing framework assertions when creating unit tests.
Personally, I wouldn't use both Debug.Assert
AND Code Contracts to enforce preconditions in newly written code - IMO Code Contracts supercede Debug.Assert
, as they offer a more comprehensive suite of checks, not to mention the benefit which can be gained from the static checking which can be performed before the code gets to run time. Maintaining duplicate precondition checks in both the Debug.Assert
and Contracts
will be cumbersome.
Rationale:
Debug.Assert
or throw
code - you can keep the existing precondition check code and terminate it with Contract.EndContractBlock()
System.Diagnostics.Debug
is built without /d:DEBUG
if you build with contract run time checking set to None
. Ref 6.2.1 in the Docs Contract.Requires
). Otherwise Contract.Assert
or Contract.Assume
can check general state, and the "guaranteed correctness" of state on leaving a method can be expressed using Contract.Ensures
. And Invariants
express that the state must be held at all times.One caveat : If you are going to write Unit Tests which deliberately violate contracts, you may need to deal with ContractException
- Jon Skeet explains this well here. e.g. Wire up a Contract.ContractFailed
handler in your test setup to a handler which calls SetHandled
and then throws a public Exception which you can catch and assert in your UT's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With