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
0
votes
1 answer

Code Contracts in Dynamics CRM 2011 Workflow

Is it possible to use Code Contracts in Dynamics CRM 2011 Workflows? The Contract Entity has the same name as Contract in System.Diagnostics.Contracts, but code contracts can still be used like this System.Diagnostics.Contracts.Contract.Requires(i…
Bvrce
  • 2,170
  • 2
  • 27
  • 45
0
votes
1 answer

CodeContracts: requires unproven: (image.PixelFormat & PixelFormat.Indexed) == 0

What does the following warning in Code Contracts for Net 4.0 mean and how to fix it??? CodeContracts: requires unproven: (image.PixelFormat & PixelFormat.Indexed) == 0 I'm doing either: var bmp = new Bitmap(pSize.Width, pSize.Height,…
Rafael Enriquez
  • 333
  • 3
  • 20
0
votes
1 answer

declare code as side-effects free using microsoft code contracts

Is there a way to declare a method as side-effects free using Microsoft Code Contracts (.net 4)?
THX-1138
  • 21,316
  • 26
  • 96
  • 160
0
votes
1 answer

ArgumentNullException Vs Contract.Requires

The ArgumentNullException throws an exception if the argument that is passed to it is null. This happens at the runtime. What does Contract.Requires do? Is it a compile time checking or checked at runtime?
wonderful world
  • 10,969
  • 20
  • 97
  • 194
0
votes
1 answer

Should I add checks for parameters which are just passed to another method?

Consider these two methods public int GetSomething(object obj) { Contract.Requires(obj != null); ... } public int GetSomethingWrapper(object anotherObj) { var obj = GetObj(anotherObj); return…
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
0
votes
1 answer

How to use Code Contracts with query?

I am fairly new to Code Contracts...and I ran into a problem. I have in a method LINQ query that go something like this: MyClass[] fields = (from p in rType.GetProperties() where p.CanRead let fAttr =…
Behemoth
  • 11
  • 4
0
votes
1 answer

Proper use of CodeContract Requires

Basically I want to know if using a code contract to determine if a key exists in a ConcurrentDictionary is an acceptable use of a code contract. It doesn't feel right to me because it's more than parameter check as it depends on the state of the…
GoClimbColorado
  • 1,060
  • 3
  • 13
  • 28
0
votes
1 answer

Repeated Code Asserting and Throwing

So far I've learned that (usually) when you assert, you should also throw. At first I wasn't sure it was right, it just made sense1, and then this answer verified. The problem I have is that it causes repeated-code (at least via contracts). Here's…
MasterMastic
  • 20,711
  • 12
  • 68
  • 90
0
votes
1 answer

Static analysis not working on simplest possible example

The following is an example I have created in order to get the static analysis tool to fail: using System.Diagnostics.Contracts; using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) …
Alex
  • 7,639
  • 3
  • 45
  • 58
0
votes
2 answers

CodeContracts Invariant is false

VS2010 keeps telling me that a CodeContract.Invariant is false. I can't see how this can possibly be the case public class BankAccountIdentifierDefinitionVariation_CreateCommandArgs : ValidatedCommandArgs { public string IdentifierCode {get;…
Peter Morris
  • 20,174
  • 9
  • 81
  • 146
0
votes
1 answer

Exclude System.Diagnostics.Contracts When Using PartCover

I am trying out the .net Code Contracts fro .net 3.5 I have some unit test that I am running PartCover over to calculate the code coverage. PartCover keeps including the System.Diagnostics.Contracts in my report. Here are the rules I am using to…
Alex Jeffery
  • 187
  • 3
  • 12
0
votes
1 answer

Why is the CC1033 message suppression not applied?

I am implementing the System.IServiceProvider.GetService method and I cannot suppress the warning ... implements interface method 'System.IServiceProvider.GetService(System.Type)', thus cannot add…
Arthur Greef
  • 319
  • 1
  • 9
0
votes
0 answers

Code Contracts- Invariants are not rewritten

I am using VS2010 Ultimate with Code Contracts. After getting unexpected runtime behavior, I reviewed the source code in reflector and realized that my invariants are not being compiled. The generated __ContractsRuntime class only contains…
smartcaveman
  • 41,281
  • 29
  • 127
  • 212
0
votes
3 answers

Code Contracts: Ensure unproven on string method

I'm playing around with code contracts and got a simple method inserting a given count of space characters between each character. e.g. Hello -> H e l l o World -> W o r l d The method InsertSpaceBetweenLetters is implemented within a class…
mbue
  • 1,591
  • 2
  • 14
  • 34
0
votes
2 answers

Why whould I want to disable .NET Code Contracts

Possible Duplicate: .net 4.0 Code Contracts. When to use? When are they a waste of time? I started to read the full documentation of the .NET Code Contracts and I noticed that you can specify the compiler not to emit the code corresponding to…
Francois Joly
  • 307
  • 2
  • 11