views:

91

answers:

2

I have been studying .NET 4.0 Code Contracts and looking on stackoverflow as well at question regarding this.

I still have never come across any sample code that uses code contracts so that gets me wondering.. is this really useful ? Or maybe its only useful one your code reaches a certain complexity? Anyone out there using Code Contracts and really glad they did?

Seems to me that all Code Contracts are is a Assertion on what goes in and what goes out of a method with the addition of being able to try to figure out the values going in and out at compile time... But then this is going to require more code on all your methods.. is it worth it ?

A benefit I noticed is it seems to me you can use code contracts kind of as a first line of unit testing... then when you write unit test to can avoid writing some of the more basic tests because the Code Contracts cover it already.. is that true ?

Will contracts work with WCF calls? I am guessing not since a proxy is created with you automatically that you cant change.

+2  A: 

I use them anytime i need to validate that an input parameter needs to have a specific value (number is positive, object is not null).

For outputs, I use them anytime I am certain that a return value should be in a certain state (not null for example).

Having contracts in the code ensures that an exception will be thrown the moment that an unexpected value crops up, and not further down in the code where objects may accidentally be left in a corrupted state because of an accidental assumption.

Personally, I think it makes the code a lot cleaner. The notation makes it a lot less to write (instead of using if(....== null)....). This way too Contract.Requires is very up front in what it is trying to accomplish. When I see that I know that the code is evaluating that a parameter is in a certain state.

Kevin
Is it really saving you time overall? What do you think is the main advantage for you over just using assertions? I worry code contracts is one of those things I put all over my code but in reality its overkill and just bloats the code since the checks are pretty obvious..I guess I am still waiting for a better example that 'SEE? It checks if the string is null or not!'
punkouter
You should be validating your inputs anyway. I think it saves time because it is readily apparent to other programmers what your intention is. You can do the same thing with if statements, but Contract.Requires shows that you are validating an input. There is no guessing involved as to what is going on. The other cool thing is that you can define how the contract works in the project file. While developing you can set the contract to stop the program immediately when a violation occurs.
Kevin
+1  A: 

there is field of study on contracts: http://en.wikipedia.org/wiki/Design_by_contract it was long before they were introduced in .net.

Code contracts are useful to give answers to following questions:

  • What does method expect?
  • What does method guarantee?
  • What does method maintain?

If you can write small and readable contract for those questions then use it.

Andrey
So if a project is started and Bob does the UI and Joe does the BLL... is this type of design such that not only will Bob and Joe exchange what a method does and the parameters.. but also i nthe UML (or whatever) the allowable values for the parameters are defined...... then you use code contracts at the top of your method to add these validations... this how it would work?
punkouter
something like that. it is just extended validation. there are tools that can extract contacts to standalone documents.
Andrey