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.

650 questions
-1
votes
1 answer
-2
votes
2 answers

Collections.sort() Comparison method violates its general contract in Java

I know that this kind of question has been asked millions of times if not billions, however I couldn't find my answer yet :) This compare() method doesn't have Long, Double, Float, ..., it only has Date, boolean, and Null checker, however it shows…
Tamer Saleh
  • 403
  • 6
  • 17
-2
votes
3 answers

Specify a parameter "may be null or not null" in Code Contracts?

I may be missing something, or I may be asking CC for something I shouldn't be, but - Is there a way to specify that a parameter "may or may not be null" without coming off as entirely redundant? For instance, imagine this contrived example: string…
user2864740
  • 54,112
  • 10
  • 112
  • 187
-2
votes
2 answers

Code contracts static checking does not seem to be working

I am using VS 2010 and testing something real basic : class Program { static void Main(string[] args) { var numbers = new int[2]; numbers[3] = 0; } } I have gone to properties > Code Contracts and have enabled the static…
sprocket12
  • 5,040
  • 16
  • 56
  • 122
-9
votes
2 answers

How to check typeof parameters in method?

How to check typeof parameters in method with help code contracts? I need check type argument in method
Mediator
  • 13,836
  • 33
  • 104
  • 177
1 2 3
43
44