Questions tagged [code-contracts]

Code Contracts is a Microsoft open source project which allows you to express pre-conditions, post-conditions, and assertions directly in code.

Code Contracts is a Microsoft open source project, originating from Microsoft Research, which allows you to express pre-conditions, post-conditions, and assertions directly in code.

A check in a Code Contract is one that, if the code is correct, should always return true. In other words: it should not test whether the database is accessible or whether the user has entered an empty string -- those are outside factors. Code Contracts are there only to make sure your code is correct and maintains its invariants.

So, typically, you would do input parameter validation on public methods by explicitly checking the value and then throwing an exception if out of range. But for private methods -- which get called only by other code, and so can expect certain pre-conditions -- the input parameters can be checked with Code Contracts.

private void MyMethod(string machineId, int age)
{
    Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(machineId));
    Contract.Requires<ArgumentOutOfRangeException>(machineId.StartsWith("PEK"));
    Contract.Requires<ArgumentOutOfRangeException>(age > 16);
    //...
}

Post-conditions are expressions that must evaluate to true when the method is finished. A typical example is to verify that the output of a function is never null, or that it is within a certain range.

private string MachineNameOf(int id)
{
    Contract.Requires<ArgumentOutOfRangeException>(id > 100);
    Contract.Ensures(Contract.Result<string>() != null);
    //...
}

GitHub: https://github.com/Microsoft/CodeContracts

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

See also (historical): http://research.microsoft.com/en-us/projects/contracts.

649 questions
4
votes
3 answers

CodeContracts issue

Hello I have a little issue regarding CodeContracts. I have a class library project which as a class with a method foo(string s1, string s2); inside the foo method, I have a Contract.Requires(s1 != null). So if I understand the meaning of my code…
Davita
  • 8,928
  • 14
  • 67
  • 119
4
votes
1 answer

Remove code contracts in Visual Studio 2017

My C# project developed in VS2015 used Code Contracts. This tool has gone stale since being open sourced by Microsoft and I don't plan to use it in VS2017. That said, when I attempt to run my project's unit tests in VS2017 it complains about the…
Mike Lowery
  • 2,630
  • 4
  • 34
  • 44
4
votes
1 answer

Can I easily exclude some contracts from static analysis?

There are certain contracts I know that the static analyzer cannot possibly prove. I can exclude certain kinds of contract violation errors from an entire function, but this is too broad-brushed. I can exclude certain violation errors by using the…
Sebastian Good
  • 6,310
  • 2
  • 33
  • 57
4
votes
3 answers

Can a [pure] function throw an exception?

Is it OK for a function that can throw an exception to have the [pure] attribute?
Jan Mattsson
  • 154
  • 2
  • 9
4
votes
2 answers

Contract based Programming

Can someone explain the concepts that Spec# might be moving into C# 4.0, regarding Code Contracts? What are code contracts (Looks to be a compile time checking pattern) should I be excited about this? Am I correct in assuming that we move what…
FlySwat
  • 172,459
  • 74
  • 246
  • 311
4
votes
0 answers

Prevent CodeContracts hiding unused method parameters in resharper

Working on a large project making use of code-contracts and resharper. Through numerous refactors some method parameters are no longer required, but code contracts appear to be hiding this. e.g. Original code may have been public foo(…
user3265613
  • 355
  • 2
  • 14
4
votes
1 answer

Eiffel: loosening the pre-conditions and tightening the post-conditions?

In Eiffel it is said that we should "loosen the pre-conditions and tightening the post-conditions", but I am not sure what this means. How does this benefit/is benefited by sub-classing? Thank you
Eli Schneider
  • 4,903
  • 3
  • 28
  • 50
4
votes
2 answers

Are unit-tests needed when you have code contracts?

If you're developing application using the code contracts, you may know, that this concept was introduced in Eiffel programming language. I have become very confused after trying this concept in my C# application using…
user4959035
4
votes
3 answers

Are Microsoft Code Contracts unsuitable for validating user input?

I've seen it written elsewhere on SO that while the Enterprise Library Validation Application Block is geared towards validating user inputs, Code Contracts are meant to prevent programmer errors. Would you support this opinion? Why?
urig
  • 16,016
  • 26
  • 115
  • 184
4
votes
3 answers

Does Sandcastle support code contracts?

My library uses code contracts. Is there a way to incorporate these contracts in the documentation generated by sandcastle?
user380719
  • 9,663
  • 15
  • 54
  • 89
4
votes
1 answer

Is this a bug in the Code Contracts rewriter?

I am experimenting with .NET Code Contracts. The following code runs just fine when runtime contract checking is turned off, but fails when runtime contract checking is turned on: using System.Collections.Generic; using…
4
votes
2 answers

C# Code Contracts: Why can't this simple condition be proven?

I'm doing a simple test of Code Contracts. The following code is in a winform. This passes (of course): private void Test(Form form) { Contract.Requires(!string.IsNullOrEmpty(form.Name)); MessageBox.Show(form.Name); } …
J Cooper
  • 16,891
  • 12
  • 65
  • 110
4
votes
2 answers

How to use Code Contracts properly?

I installed a code contracts from original site and tried to write some sample code. But R# just writes that Method invocation is skipped. When I watch decompiled sources I see that method is conditional: CONTRACTS_FULL constant must be defined. I…
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
4
votes
1 answer

Writing Code Contracts on Methods Raising Events

I am writing code contracts on an interface that uses events: interface EventInterface { event EventHandler ItemAdded; bool Add(T item); } When an item is added to a collection that implements the interface, the collection must raise an…
Mikkel R. Lund
  • 2,336
  • 1
  • 31
  • 44
4
votes
1 answer

Code contracts .net - alternatives

Recently, I started to use Code contracts for .net. The idea of code contracts itself is great in my opinion, but the implementation is very unpleasant. The main reasons I don't like it are: I can use only methods like Contract.Require() inside my…
Alex Butenko
  • 3,664
  • 3
  • 35
  • 54