I'd prefer examples to be in a Lisp variant (bonus points for Clojure or Scheme) since that's what I'm most familiar with, but any feedback regarding DBC in functional lanugages would of course be valuable to the greater community.
Here's an obvious way:
(defn foo [action options]
(when-not (#{"go-forward" "go-backward" "turn-right...
hi
I'm reading about dbc (http://en.wikipedia.org/wiki/Design%5Fby%5Fcontract)
Can someone please give me a simple example of using class invariants in relation to inheritance?
...
I have been reading over design-by-contract posts and examples, and there is something that I cannot seem to wrap my head around. In all of the examples I have seen, DbC is used on a trivial class testing its own state in the post-conditions (e.g. lots of Bank Accounts).
It seems to me that most of the time when you call a method of a ...
I'll best just show with a code example what I would like to accomplish?
class SomeClass
{
public int SomeProperty;
public void SomeOperation()
{
Contract.Ensures( "SomeProperty's value has not changed." );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// How can I write t...
I have a project where the client is using Entity Framework, and I'm trying to abstract away the generated classes from the rest of the application.
One generated class is Category and it has say Type as a property.
I've created an interface that I want Category to implement, like this:
public interface ICategory
{
string Type { g...
We've got an OO codebase where in quite a lot of cases hashcode() and equals() simply don't work, mostly for the following reason:
There is no way to extend an
instantiable class and add a value
component while preserving the equals
contract, unless you are willing to
forgo the benefits of object-oriented
abstraction.
Tha...
To convince my team mates of the usefulness of design-by-contract, I am looking for well known examples of realistic systems that were built using design by contract. Pointers to realistic, critical evaluations are appreciated.
In other words: what is the best example of a system successfully built using design-by-contract?
...
Contrary to Code Contracts in C#, in JML Code Contracts are just text that's used in the form of comments in the header of a method. Wouldn't it be better to have them exposed as Annotations, then? That way even when compiling the information would persist on the .class's metadata, contrary to comments, that get erased.
Am I missing som...
I've tried before some Eclipse plugin (I don't remember its name) but it was lacking a lot of features (generics, for example). Is there anything easy to set up that would run on the last version of Java?
Thanks
...
For the purpose of this discussion, there are two kinds of parameters an object constructor might take: state dependency or service dependency. Supplying a service dependency with an IOC container is easy: DI takes over. But in contrast, state dependencies are usually only known to the client. That is, the object requestor.
It turns ...
I don't see much of the developer using Java Assert, but I am very keen in using them. Could you share some tips to effectively use them?
...
At face value, it would seem that object initializers present a problem for .net 4.0 "code contracts", where normally the invariant should be established by the time the object constructor is finished. Presumably, however, object-initializers require properties to be set after construction is complete.
My question is if the invariants ...
I am implementing my own ArrayList for school purposes, but to spice up things a bit I'm trying to use C# 4.0 Code Contracts. All was fine until I needed to add Contracts to the constructors. Should I add Contract.Ensures() in the empty parameter constructor?
public ArrayList(int capacity) {
Contract.Requires(capacity > 0);...
I have a bit of code with the following logic:
//pseudo-code
foreach (element in elementList) {
if (element is whatever)
return element;
}
}
In theory, there is always one element that is whatever, so this method should pose no problems. In any case, I've put an assertion on the end of the method just to be sure:
//ps...
I want to jump into coding by contract. I got VS2010 (with the C# 4.0 compiler) but I have to target the 3.5 framework.
What 3rd party code by contract library has classes and interface the most like the .NET 4.0 ones?
...
These days I'm used to checking every single precondition for every function since I got the habit from an OS programming course back at uni.
On the other hand, at the software engineering course we were taught that a common precondition should only be checked once, so for example, if a function is delegating to another function, the fi...
Consider this immutable type:
public class Settings
{
public string Path { get; private set; }
[ContractInvariantMethod]
private void ObjectInvariants()
{
Contract.Invariant(Path != null);
}
public Settings(string path)
{
Contract.Requires(path != null);
Path = path;
}
}
Two th...
I am looking at using design by contract for a personal project in C# using .NET 4.
I have been reading about Microsoft's attempt at a design by contract framework:
http://msdn.microsoft.com/en-us/devlabs/dd491992.aspx
But there are many alternatives and I'm baffled - so which is the best and why? What are the pros and cons of each?
...
This is my code:
public class RegularPolygon
{
public int VertexCount;
public double SideLength;
public RegularPolygon(int vertexCount, double sideLength)
{
Contract.Requires(vertexCount >= 3);
VertexCount = vertexCount;
SideLength = sideLength;
}
[ContractInvariantMethod]
private vo...
Hi All,
I'm using the Code Contracts classes in the System.Diagnostics.Contracts namespace to define some contracts for my objects and I'm wondering how others name their contract classes when the contract is defined against a base interface. Let me illustrate with a small example:
[ContractClass(typeof(AnimalContract))]
public interfa...