BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Code Contracts for .NET Is Available for Download

Code Contracts for .NET Is Available for Download

Leia em Português

This item in japanese

Bookmarks

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.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • Impressive

    by Mohamed Faramawi,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT