InfoQ

InfoQ

News

My Bookmarks

Login or Register to enable bookmarks for unlimited time.

The content has been bookmarked!

There was an error bookmarking this content! Please retry.

Code Contracts for .NET Is Available for Download

Posted by Abel Avram on Feb 24, 2009

Sections
Development,
Architecture & Design
Topics
Programming ,
.NET Framework ,
.NET ,
Unit Testing ,
Software Testing
Tags
Design by Contract ,
API

Code Contracts is the .NET implementation of the Design by Contract concept. While it was supposed to be delivered with .NET 4.0, Code Contracts is already available for download from DevLabs. Contracts impose certain restrictions on using APIs, making programming safer, having more validations and resulting in fewer unexpected errors during runtime.

Design by Contract, or Programming by Contract, is not a new concept in programming, many languages already having such implementations. Code Contract brings this programming paradigm to .NET. The basic idea is to establish a contract between the two parties involved: the client (caller) and the supplier (API called). By adhering to the terms of the contract, the client is more confident the supplier will supply expected results, and the supplier will be more confident that the client will make appropriate calls.

There are three types of contract conditions available for Code Contracts: pre-conditions, object invariants, and post-conditions. Pre-conditions are used, for example, to verify that a constructor or a method is called using the right parameter values. A post-condition is used to verify that a value returned is appropriate. Object invariants are used to enforce certain conditions on objects to make sure they don’t obtain illegal values.

While this sounds very similar to unit testing, what’s news is that those validations can be performed at build time. The compiler will make specific checks via Contracts to ensure that all the conditions specified are met, issuing warning if they are not. The programmer can immediately see if a condition is not going to be met and take corrective measures before running the code.

Currently, the DevLabs team works on including Contracts in the documentation generated for projects. By reading an API documentation, anyone will be able to see all the contract conditions specified for it.

The end result of using Code Contracts is better and safer code, more appropriate usage of external APIs, in the end better software.

Useful links: Code Contracts online documentation, Microsoft Research Code Contracts website, related forum.

Impressive by Mohamed Faramawi Posted
  1. Back to top

    Impressive

    by Mohamed Faramawi

    I think this will minimize the need to write unit tests to validate method behaviors when an invalid input is passed as passing an invalid input is not supported.
    It looks like injected unit tests in runtime code.. however the contracts should be unit tested somehow.. but i don't think it will be that hard.
    Can't wait to use this

Educational Content

New-age Transactional Systems - Not Your Grandpa's OLTP

John Hugg discusses high volume transaction processing applications with high and low frequency profiles, and how VoltDB can be used for that purpose.

Cool Code

Kevlin Henney examines code samples to see what can be learned from them starting from the premise that one won’t write great code unless he knows how to read it.

Collaboration: At the Extremities of Extreme

Jason Ayers share the observations he made watching a team of developers collaborating in real time on the same code base, pushing XP, pair programming and continuous integration to their extremes.

Yesod Web Framework

Michael Snoyman presents Yesod, a web framework written in Haskell and containing a web server, templating, ORM, libraries (templating, gravatar, etc.).

Transactions without Transactions

Richard Kreuter and Kyle Banker on how to avoid classical RDBMS transactional systems by using compensation mechanisms, transactional messaging or transactional procedures.

Attila Szegedi on JVM and GC Performance Tuning at Twitter

Attila Szegedi talks about performance tuning Java and Scala programs at Twitter: how to approach GC problems, the importance of asynchronous I/O, when to use MySQL/Cassandra/Redis, and much more.

10 tips on how to prevent business value risk

One category of risk that project teams need to ensure they address is business value failure – delivering a product that fails to provide value for the business investor.

Interview: Software Systems Architecture: Working With Stakeholders Using Viewpoints and Perspectives

InfoQ spoke to the authors of Software Systems Architecture on a couple of new topics, the System Context viewpoint and Agile, which have been added to the second edition.