Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Design By Contract' in C#

I wanted to try a little design by contract in my latest C# application and wanted to have syntax akin to:

public string Foo() {     set {         Assert.IsNotNull(value);         Assert.IsTrue(value.Contains("bar"));         _foo = value;     } } 

I know I can get static methods like this from a unit test framework, but I wanted to know if something like this was already built-in to the language or if there was already some kind of framework floating around. I can write my own Assert functions, just don't want to reinvent the wheel.

like image 462
IAmCodeMonkey Avatar asked Nov 04 '08 03:11

IAmCodeMonkey


People also ask

What is Design by Contract in programming?

Created by Bertrand Meyer in the 1980s, Design by Contract (DbC) is an approach to software design that focuses on specifying contracts that define the interactions among components.

What is Design by Contract C#?

C# 4.0 Code Contracts. Microsoft has released a library for design by contract in version 4.0 of the . net framework. One of the coolest features of that library is that it also comes with a static analysis tools (similar to FxCop I guess) that leverages the details of the contracts you place on the code.

Why is Design by Contract useful?

In such cases, DbC makes the supplier's job easier. Design by contract also defines criteria for correctness for a software module: If the class invariant AND precondition are true before a supplier is called by a client, then the invariant AND the postcondition will be true after the service has been completed.

What are contracts in C?

What is a contract? A contract specifies in a precise and checkable way interfaces for software components. These software components are typically functions and methods, that have to fulfil preconditions, postconditions, and invariants.


2 Answers

C# 4.0 Code Contracts

Microsoft has released a library for design by contract in version 4.0 of the .net framework. One of the coolest features of that library is that it also comes with a static analysis tools (similar to FxCop I guess) that leverages the details of the contracts you place on the code.

Here are some Microsoft resources:

  • The main Microsoft Research site
  • The user manual
  • The 2008 PDC presentation
  • The 2009 PDC presentation

Here are some other resources:

  • Code Contracts for .NET 4.0 - Spec# Comes Alive
  • .NET Code Contracts and TDD Are Complementary
  • Code Contracts Primer – Part 5: Utilizing Object Invariants
  • Code Contracts Primer – Part 6 Interface Contracts
like image 155
5 revs Avatar answered Nov 05 '22 12:11

5 revs


Spec# is a popular microsoft research project that allows for some DBC constructs, like checking post and pre conditions. For example a binary search can be implemented with pre and post conditions along with loop invariants. This example and more:

 public static int BinarySearch(int[]! a, int key)     requires forall{int i in (0: a.Length), int j in (i: a.Length); a[i] <= a[j]};     ensures 0 <= result ==> a[result] == key;     ensures result < 0 ==> forall{int i in (0: a.Length); a[i] != key};  {    int low = 0;    int high = a.Length - 1;     while (low <= high)      invariant high+1 <= a.Length;      invariant forall{int i in (0: low); a[i] != key};      invariant forall{int i in (high+1: a.Length); a[i] != key};    {      int mid = (low + high) / 2;      int midVal = a[mid];       if (midVal < key) {        low = mid + 1;      } else if (key < midVal) {        high = mid - 1;      } else {        return mid; // key found      }    }    return -(low + 1);  // key not found.  } 

Note that using the Spec# language yields compile time checking for DBC constructs, which to me, is the best way to take advantage of DBC. Often, relying on runtime assertions becomes a headache in production and people generally elect to use exceptions instead.

There are other languages that embrace DBC concepts as first class constructs, namely Eiffel which is also available for the .NET platform.

like image 30
Jim Burger Avatar answered Nov 05 '22 10:11

Jim Burger