Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contract.Requires usage

Here is my problem. I am a very big fan of Design by contract, I am using this concept especially when developing libraries that can be used by other developers. I just found out a new way of doing this which is: Contract.Requires instead of Exception: So instead of having:

public void SomeMethod(string name){    if(name==null) throw new NullArgumentException("Null values not supported"); }  

I now have:

public void SomeMethod(string name){    Contract.Requires(name != null); } 

EDIT: I am working under VS2010 on debug mode.

Problem: Contract.Requires does not do anything, even when name is null!

The MSDN documentation says:

Specifies a precondition contract for the enclosing method or property.

But nothing is specified in case the condition is not met!

I also noticed there are other Contract.Requires overloads that throw exception, display message... but then what is Contract.Requires(Boolean) for?

EDIT Answer below highlighted that a plug-in must be installed to have the full power of Contract API but then what about Mono users who want their code to behave the same on different platforms?

like image 644
GETah Avatar asked Nov 28 '11 18:11

GETah


People also ask

What is required for a contract?

A contract is an agreement between parties, creating mutual obligations that are enforceable by law. The basic elements required for the agreement to be a legally enforceable contract are: mutual assent, expressed by a valid offer and acceptance; adequate consideration; capacity; and legality.

What is the use of contract?

The main purpose of a contract is to formalize new relationships and outline the various legal obligations each party owes to the other. Today, most contracts are agreed between businesses, not people.

What are the 7 elements of a contract?

For a contract to be valid and recognized by the common law, it must include certain elements— offer, acceptance, consideration, intention to create legal relations, authority and capacity, and certainty. Without these elements, a contract is not legally binding and may not be enforced by the courts.


2 Answers

You should do the following:

  1. Install the Code Contracts add-in as nfechner has noted
  2. Go to the project properties, 'Code Contracts' folder
  3. Check 'Perform Runtime Contract Checking'
  4. Switch 'Assembly Mode' to 'Standard Contract Requires'
  5. Substitute your Contract.Requires with Contract.Requires<SomeException> (the first one throws System.Diagnostics.ContractException while the second throws the exception you specified which is important for public methods)

That's the basic setup. For more accurate configuration, refer to the manual

If you use Mono, probably, Contract class is empty. I haven't done this, but chapter seven from the Contracts manual seems to explain how to provide your own implementation.

like image 130
Pavel Gatilov Avatar answered Oct 02 '22 07:10

Pavel Gatilov


From the Contract class docs:

Important

You must install a Visual Studio add-in to enforce contracts. The Code Contracts Premium Edition add-in lets you specify static and run-time checking of code contracts on the project Properties page. If you do not enable run-time checking, contracts such as the Contract.Ensures method will not throw exceptions during run time if a contract is violated. The Visual Studio add-in does not ship with Visual Studio 2010 or the Windows SDK.

like image 21
nfechner Avatar answered Oct 02 '22 07:10

nfechner