Questions tagged [code-contracts]

Code Contracts provide a language-agnostic way to express coding assumptions in .NET programs. The contracts take the form of preconditions, postconditions, and object invariants. Contracts act as checked documentation of your external and internal APIs.

Contracts is Microsoft's reference implementation of the "Design by contract" methodology. According to Microsoft Research, "Code Contracts bring the advantages of design-by-contract programming to all .NET programming languages." They can be checked statically at compile time or dynamically at run time, or be used to create unit tests semi-automatically.

The bulk of its functionality is implemented as a set of static library methods for writing pre-conditions, post-conditions and invariants; this allows all .NET languages to write contracts without any special extensions. All contracts are expressed as static calls to the methods Contract.Requires and Contract.Ensures. Therefore, normal utilities of the .NET tool chain such as Intellisense and compile-time checking can be applied to the task of writing contracts.

Like all language or library tags, this tag should be used for questions about issues in using this tool for writing code, or about its suitability for a particular purpose.

12 questions
55
votes
10 answers

Should a method validate its parameters?

Say you are designing a Square root method sqrt. Do you prefer to validate the parameter passed is not a negative number or do you leave it up to the caller to make sure the param passed is valid. How does your answer vary if the method/API is for…
Amit Wadhwa
  • 1,962
  • 1
  • 15
  • 20
27
votes
4 answers

Why would I use code contracts

I recently stumbled upon Microsoft's framework for code contracts. I read a bit of documentation and found myself constantly asking: "Why would I ever want to do this, as it does not and often cannot perform a static analysis." Now, I have a kind of…
Falcon
  • 19,248
  • 4
  • 78
  • 93
19
votes
2 answers

When to use [Pure] on a constructor?

I'm learning about code contracts in .NET, and I'm trying to understand the idea of pure constructors. The code contracts documentation states: All methods that are called within a contract must be pure; that is, they must not update any…
p.s.w.g
  • 4,135
  • 4
  • 28
  • 40
13
votes
6 answers

Contract Based Programming vs Unit Test

I am a somewhat defensive programmer and a big fan of Microsofts Code Contracts. Now I cannot always use C# and in most languages the only tool I have is assertions. So I usually end up with code like this: class { function() { …
ronag
  • 1,179
  • 1
  • 10
  • 18
10
votes
2 answers

code contracts/asserts: what with duplicate checks?

I'm a huge fan of writing asserts, contracts or whatever type of checks available in the language I'm using. One thing that bothers me a bit is that I'm not sure what the common practice is for dealing with duplicate checks. Example situation: I…
stijn
  • 4,108
  • 1
  • 25
  • 31
9
votes
4 answers

Handling changes in a event-driven microservice architecture

I'm doing an research-project where I'm researching the options to handle changes in an event-driven microservice architecture. So, let's say we got an application where we got four different services. Each of these services has an own database to…
6
votes
6 answers

Should I assert the preconditions of functions in a public API?

I am writing a library for some data structures in C that will be used in embedded systems. I have had issues designing and coming up with a solid error handling plan. This API is only subject to logic errors which is why I am so conflicted. By this…
6
votes
1 answer

What are the practical examples of code exploration techniques?

Code Exploration (CE) is quite a new term and I wonder if there already any successful examples of implementing this techniques in terms of Continuous Integration principles? In short, Code Exploration can be described as the process of testing your…
4
votes
2 answers

Do .NET 4.0 Code Contracts have a role to play in solo-programmer projects?

I often find myself wondering what programming best practices apply to solo programming, since most of the time, I'm the only programmer on a project. I just started experimenting with C# 4.0 Code Contracts, and I thought I'd ask the community what…
devuxer
  • 666
  • 6
  • 13
3
votes
4 answers

Why must the burden of proof rest with the caller and not the method of the class that is being called?

Michael Perry states in his Pluralsight course on Provable Code (transcript subscription only) that: [T]he burden of proof rests with the caller In a code contract, why must the burden of proof rest with the caller and not the method of the class…
Phil C
  • 1,956
  • 1
  • 18
  • 34
1
vote
2 answers

Interfaces for CRUD Classes

In an in-house solution I've been working on, I've been unable to understand the benefit of how interfaces are frequently implemented throughout the project. Which is as follows: Want to do CRUD operations on a database for a specific model/table?…
8protons
  • 1,359
  • 1
  • 10
  • 27
1
vote
1 answer

Contract decoupling on microservices

I am not trying to launch a Microservices vs SOA debate but I find hard to understand the following statement: "Microservices architecture does not support contract decoupling, whereas contract decoupling is one of the primary capabilities offered…
Cris
  • 303
  • 2
  • 9