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
1
vote
1 answer

Temporarily disabling Code Contract

Code Contracts allow for contracts to be defined on interfaces, such as IList. Some of these, like the a fore mentioned one, have these implemented in .Net already. I'm creating a class that inherits from IList and I'm writing tests to cover…
Xilconic
  • 3,785
  • 4
  • 26
  • 35
1
vote
1 answer

Getting up and running with code contracts

In VS2010 and .NET 4.0, I see the shortcuts in intellisense for adding contracts to my code (Eg cr, crr) but when I tab to add these in, the code (Such as Contract.Requires) does not have the valid assembly so there is no intellisense (The type…
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
1
vote
1 answer

Proving post conditions for code that does not implement Code Contracts

I'm learning how to design by contract by annotating my existing code with Contract.Requires() and Contract.Ensures() wherever I can. After fixing the warnings that were actually caused by me, I started to notice that many methods in the framework…
Steven Liekens
  • 13,266
  • 8
  • 59
  • 85
1
vote
0 answers

Is there any way to temporarily disable CCCheck in vs2013?

I have a vs solution with 20 or so projects, when I compile it runs CCCheck in the background and slows down my compilation (unless I go to processes and repeatedly kill CCCheck.exe). In vs2013 project properties I could not find a way to disable…
Samuel
  • 1,949
  • 4
  • 18
  • 30
1
vote
1 answer

How to create contract class for class with required constructor arguments?

I have an abstract class whose constructor requires a parameter. The parameter cannot be null. // the abstract class [ContractClass(typeof(AbstractClassContract))] public abstract class AbstractClass { // Constructor with required parameter …
Keith
  • 20,636
  • 11
  • 84
  • 125
1
vote
0 answers

Could PureAttribute only be guaranteed when manipulating primitive types?

JetBrains annotations: Indicates that a method does not make any observable state changes. The same as System.Diagnostics.Contracts.PureAttribute Microsoft Code Contracts: Indicates that a type or method is pure, that is, it does not make any…
Den
  • 1,827
  • 3
  • 25
  • 46
1
vote
2 answers

Can I change the order of my post compilers?

How do I change the order of my post compilers in visual studio? Specifically, I would like to make the code contracts post compililation happen AFTER Postsharp. By default it's the other way around.
Ev.
  • 7,109
  • 14
  • 53
  • 87
1
vote
3 answers

Is there any way how to test contracts set using Code contracts?

I would like to write unit test that verify that my method does not accept invalid arguments. Validity of arguments is checked using Code Contract's Contract.Requires call. Why would I want to test contracts? I consider my tests to be kind of method…
Rasto
  • 17,204
  • 47
  • 154
  • 245
1
vote
1 answer

Code Contract and ensuring no exceptions are thrown

I was wondering if I'm able to use Microsoft Code Contracts to verify the invariant that a method shall not throw any exception. The use case is as follows: I have an importer with a logger attached to it, where the importer uses a base class for…
Xilconic
  • 3,785
  • 4
  • 26
  • 35
1
vote
1 answer

How to express that a method in a library is pure?

Given the case when you need to call a method of a library that you can't modify in a Contract.Requires, (v.g., the method Regex.Matches) there is a way to indicate this method is pure? Contract.Requires(Regex.Matches(password,…
Apocatastasis
  • 500
  • 1
  • 9
  • 21
1
vote
2 answers

How unit test on Code Contract should look like?

I have this method in ContractClass: public UserModel GetUser(string groupName, string login) { Contract.Requires(groupName != null); Contract.Requires(groupName != string.Empty); Contract.Requires(login != null); …
Vladimir Nani
  • 2,774
  • 6
  • 31
  • 52
1
vote
1 answer

How to force an invariant method throw a particular exception in C# Code Contracts?

I want my object invariant method to throw a specific exception. Does it make sense? Is it possible in C#? For instance, I have the following code, including class A with invariant method and exception class E. For now class E is not participating…
Hoborg
  • 891
  • 12
  • 21
1
vote
2 answers

Using codeContracts in .net 4.0 without affecting other developers

I am new to codeContracts,I like it because it seems clean. I dont like the fact that I need to download a library to make it work.Never understood why microsoft didnt make it part of the framework as whole. I dont want to impose other collegues in…
user9969
  • 15,632
  • 39
  • 107
  • 175
1
vote
1 answer

Code Contracts not applied using ContractClassFor

This one has me stumped. I am using Code Contracts in a scenario where methods to which the contracts apply are interface implementations. So, I am using the abstract ContractClassFor approach to define the contract requirements. This works fine…
sydneyos
  • 4,527
  • 6
  • 36
  • 53
1
vote
1 answer

Contracts design pattern in C

Is there a library/framework/article that describes ways to use contracts in C I mean code that is more then obvious asserts for example somting like: Programming with Contracts in C++ but for C
alexpov
  • 1,158
  • 2
  • 16
  • 29