Off the top of my head, I can think of 4 ways to check for null arguments:
Debug.Assert(context != null);
Contract.Assert(context != null);
Contract.Requires(context != null);
if (context == null) throw new ArgumentNullException("context");
I've always used the last method, but I just saw a code snippet that used Contract.Requires
, which I'm unfamiliar with. What are the advantages/disadvantages of each method? Are there other ways?
In VS2010 w/ Resharper,
Contract.Assert
warns me that the expression is always true (how it knows, I'm not quite sure... can't HttpContext be null?), Contract.Requires
gets faded out and it tells me the compiler won't invoke the method (I assume because of the former reason, it will never be null), and context != null
all the code following gets faded out and it tells me the code is heuristically unreachable. So, it seems the last 3 methods have some kind of intelligence built into the VS static checker, and Debug.Assert
is just dumb.
The preconditions for a test case include the state a system and its environment must be before a specific test can be run. In other words, preconditions specify the setup needed for a test case to be executed successfully.
Disadvantages. Dynamic testing is time-consuming as the application/software or code needs a lot of resources is executed. Dynamic testing increases project/product costs because the program does not begin early in the software lifecycle, and any problems that are resolved later can, therefore, lead to a cost increase.
My guess is that there is a contract applied to the interface IHttpHandler.ProcessRequest which requires that context != null. Interface contracts are inherited by their implementers, so you don't need to repeat the Requires. In fact, you are not allowed to add additional Requires statements, as you are limited to the requirements associated with the interface contract.
I think it's important to make a distinction between specifying a contractual obligation vs. simply performing a null check. You can implement a null check and throw an exception at runtime, as a way to inform the developer that they are using your API correctly. A Contract expression, on the other hand, is really a form of metadata, which can be interpreted by the contract rewriter (to introduce the runtime exceptions that were previously implemented manually), but also by the static analyzer, which can use them to reason about the static correctness of your application.
That said, if you're working in an environment where you're actively using Code Contracts and static analysis, then it's definitely preferable to put the assertions in Contract form, to take advantage of the static analysis. Even if you're not using the static analysis, you can still leave the door open for later benefits by using contracts. The main thing to watch out for is whether you've configured your projects to perform the rewriting, as otherwise the contracts will not result in runtime exceptions as you might expect.
To elaborate on what the commenters have said, the difference between Assert, Assume and Requires is:
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