Visual Studio, C# : Why should or should not you use Code Contracts #Root #By Sayed Ahmed

http://programmers.stackexchange.com/questions/211337/why-would-i-use-code-contracts

Why Use?

To avoid null reference exceptions

To avoid critical and costly failure of code/software

However, code may look over crowded, and can be difficult to maintain and extend if you use Contracts.

 

If yours is not a critical application, and in general, failures can never be that costly for you: then you might not need to use it everywhere ().

 

However, be careful in writing your code, deal (address) with the cases where the objects might be null, additionally, properly initialize your variables.

 

Read the page as provided in the url above.

 

Related Tools: If you want to use Contracts, you need to install both of the tools below:

 

https://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970

https://visualstudiogallery.msdn.microsoft.com/02de7066-b6ca-42b3-8b3c-2562c7fa024f

 

That’s right I had to install to work with.

After you install, you will see a tab Code Contracts in the project properties.

 

The useful part:

Contracts vs. Null Object pattern

Now this is at least in the same ball park. Languages like Scala and Haskell has had great success with this approach to eliminating null references entirely from programs. (Even if Scala formally allows nulls the convention is to never use them)

If you already employ this pattern to eliminate NREs you’ve basically removed the largest source of runtime failures there is in basically the manner contracts allow you to do it.

The difference might be that contracts has an option to automatically require all your code to avoid null, and thus force you to use this pattern in more places to pass compilation.

On top of that contracts also give you the flexibility to target things beyond null. So if you no longer see any NRE in your bugs you might want to use contracts to strangle the next most common issue you might have. Off by one? Index out of range?

But…

All that being said. I do agree that the syntactic noise (and even structural noise) contracts add to the code is quite substantial and the impact the analysis has on your buildtime should not be underestimated. So if you decide to add contracts to your system it would probably be wise to do so very carefully with a narrow focus on which class of bugs one tries to address.

 

 

Read this as well:

http://research.microsoft.com/en-us/projects/contracts/faq.aspx

  From: http://sitestree.com/?p=2115
Categories:Root, By Sayed Ahmed
Tags:
Post Data:2015-07-25 08:51:38

Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada