code-contracts

Has someone experiences with metrics for Design-By-Contract or can recommend metrics to measure the usage of Design-By-Contract in a code base?

We are currently introducing Design-by-Contract to a software development group of about 60 developers, which are developing different components. We started by defining Design-By-Contract policies for C# and Java. To measure the progress we are counting the number of classes and the number of contract assertions (Preconditions, post con...

Resharper and Code Contracts not playing well together

I'm using Resharper 5.x to do compile-time analysis and it's usually pretty good about it, but it doesn't seem to be applying code contracts to its logic. I have something like the following, but I'm getting an issue on the marked line. public void Method(int arg) { Contract.Requires(this.NullableValueType != null); this.Method...

Code contracts problem

Consider the following code : public class RandomClass { private readonly string randomString; public RandomClass(string randomParameter) { Contract.Requires(randomParameter != null); Contract.Ensures(this.randomString != null); this.randomString = randomParameter; } public string R...

Ensures Unproven via property when implementing interface

I'm trying what, to me, seems like some fairly basic code contracts code. I've reduced it down to the following problem. The following fails the static analysis, with the message CodeContracts: ensures unproven: this.Frozen using System; using System.Diagnostics.Contracts; namespace PlayAreaCollection2010 { public class Stri...

Should we allow null/empty parameters?

I recently had a discussion with a co-worker on whether we should allow null or empty collections to be passed as method parameters. My feeling is that this should cause an exception, as it breaks the method's 'contract', even if it does not necessarily break the execution of the method. This also has the advantage of 'failing fast'. My ...

How to use code contracts when deriving from interfaces like IDictionary<T, U>?

One class I am writing implements IDictionary<string, object>. In my CopyTo implementation, I would like to use code contracts: stuff like Contract.Requires<ArgumentNullException>(array != null). But, I get this warning (with some namespaces removed for readability): Method 'LuaDictionary.CopyTo(KeyValuePair<String,Object>[],Int32)' im...

Collection Contracts and Threading

Suppose I have a custom collection class that provides some internal thread synchronization. For instance, a simplified Add method might look like this: public void Add(T item) { _lock.EnterWriteLock(); try { _items.Add(item); } finally { _lock.ExitWriteLoc...

How can I specify code contracts for existing framework (BCL) code?

Code contracts work great until you have to add a bazillion Contract.Assume(...) for the results coming out of framework code. For instance, MemoryStream.ToArray() never returns a null array, as best as I can tell from looking at it in Reflector, but it's not documented as a contract, so I have to Assume it everywhere. Is there a magica...

Code Contracts: ContractClassFor when dealing with a generic abstract class?

So, I have a little problem here. Suppose I have: public class Repository<TEntity> where TEntity : class { public abstract void Add(TEntity entity); // ...and so on... } And now I want to define a contract class, like so: public class RepositoryContracts<TEntity> : Repository<TEntity> where TEntity : class { pub...

How to tell the static checker that a property never changes when it is unprovable?

My class has a property that is initialized in the constructor and is never supposed to change. Methods all around my codebase accept that class as a parameter and rely on that property satisfying a certain condition. The key point is that it is really unprovable that the property never changes. I can personally "swear" that it never ch...

Contract.ForAll doesn't work?

I'm trying to use Contract.ForAll, and it looks like I'm missing something here. Consider this small example: var l = new List<string>(); Contract.Assume( Contract.ForAll( l, s => s != null ) ); foreach ( var s in l ) Console.WriteLine( s.Length ); Despite the Contract.Assume call, I do get a "possible callin...

Create code contracts for a legacy library

The ultimate goal is to specify contracts for a class that resides in an external assembly that I don't have control over (i.e. I cannot just add contracts to that class directly). What I have tried so far: ContractClassFor attribute. Doesn't work, because the target class must point back to the contract class. Manually construct a ...

Are Code contracts worth anything?

I don't want to insult anyone, but has anyone ever gotten anything useful out of using code contracts? Well, maybe except a massive head ache. I've only tried Code contracts on a small library of mine, and all I felt was I kept repeating over and over again, that the parameters were != null. Then after stating some parameter p was != nul...

Programming by contracts in php

Programming by contracts is a modern (?) trend in .net. But what about libraries/frameworks for code contracts in php? What do you think about applicability of this paradigm for php? Googling of "code contracts php" gave nothing to me. UPD: for those who didn't even try to google for unknown term ("coding by contracts") and voted fo...

Turn off code contracts warning

I want to turn off a code contract warning, but only for specific code lines. How do I do that? For instance, I get: Warning 87 CodeContracts: requires unproven: key != null for: return HttpContext.Current.Items[typeof(T).AssemblyQualifiedName]; which will never happen in our applications. ...

funny C# Contract.Requires bug with comments?

Hi, I had the following code: Contract.Requires(somecondition, "some message"/*some comment*/); and while debugging at some moment the condition wasn't met, and it threw the exception with the usual text, plus the comment! :P So I got this exception: ContractException was unhandled. Precondition failed: somecondition. "some message...

Specify code contract on Func<T> parameters?

Say I have the following public T Example(Func<T> f) { Contract.Requires(f != null); Contract.Requires(f() != null); // no surprise, this is an error ... } Is there any way to specify that my Func<T> parameters must obey some contracts? ...