Questions tagged [design-by-contract]

Design by Contract (DbC) or Programming by Contract is an approach to designing computer software. It prescribes that software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. These specifications are referred to as "contracts", in accordance with a conceptual metaphor with the conditions and

Design by Contract (DbC) or Programming by Contract is an approach to designing computer software. It prescribes that software designers should define formal, precise and verifiable interface specifications for software components, which extend the ordinary definition of abstract data types with preconditions, postconditions and invariants. These specifications are referred to as "contracts", in accordance with a conceptual metaphor with the conditions and obligations of business contracts.

Because Design by Contract is a registered trademark of Eiffel Software in the United States, many developers refer to it as Programming by Contract, Contract Programming, or Contract-First development.

216 questions
10
votes
3 answers

What is the idiomatic way to check and document function preconditions and postconditions in R?

What is the idiomatic way to check and document function preconditions and postconditions in R? I would consider Eiffel's built in require and ensure constructs or D's in and out blocks state of the art here, but most languages don't have these…
fmark
  • 57,259
  • 27
  • 100
  • 107
9
votes
5 answers

What are the best practices for Design by Contract programming

What are the best practices for Design by Contract programming. At college I learned the design by contract paradigma (in an OO environment) We've learned three ways to tackle the problem : 1) Total Programming : Covers all possible exceptional…
Peter
  • 47,963
  • 46
  • 132
  • 181
9
votes
3 answers

Code Contracts, will you use them?

Microsoft just released Code Contracts, a tool that integrates with Visual Studio and allows you to define contracts for your .Net code and get runtime and compile time checking. Watch the video on Channel 9 that shows how it being used. For now…
Renaud Bompuis
  • 16,596
  • 4
  • 56
  • 86
9
votes
2 answers

Design By Contract LIbrary(ies) for Common Lisp?

Coming from a background in Clojure, I am taken with the potential that its pre-/post-conditions provide as a basis for design by contract: ;; sqr.clj (defn sqr [n] {:pre [(not= 0 n) (number? n)] :post [(pos? %) (number? %)]} (* n n)) (sqr…
fogus
  • 6,126
  • 5
  • 36
  • 42
9
votes
2 answers

Why isn't JML implemented as Annotations in Java?

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…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
9
votes
8 answers

How can I place validating constraints on my method input parameters?

Here is the typical way of accomplishing this goal: public void myContractualMethod(final String x, final Set y) { if ((x == null) || (x.isEmpty())) { throw new IllegalArgumentException("x cannot be null or empty"); } if…
Robert Campbell
  • 6,848
  • 12
  • 63
  • 93
8
votes
2 answers

When to use assert in client & common GWT code

There are several questions on StackOverflow discussing the question of when one should use an assert statement versus throwing some exception. (Examples here, here, here, here, and here. However, I have come to suspect that the conventional wisdom…
pohl
  • 3,158
  • 1
  • 30
  • 48
8
votes
3 answers

Is a postcondition a (type of) unit test?

I'm trying to incorporate some design-by-contract techniques into my coding style. Postconditions look a lot to me like embedded unit tests and I'm wondering if my thinking here is on the right track or way off-base. Wikipedia defines a…
glenn
  • 651
  • 1
  • 6
  • 14
7
votes
3 answers

How does Racket Scheme's "design by contract" features different from Eiffel?

I know that both Eiffel (the progenitor) and Racket both to implement "Design by Contract" features. Sadly, I am not sure how one would different from the other. Eiffel's DBC is reliant on the OOP paradigm and inheritance, but how would Racket, a…
Eli Schneider
  • 4,903
  • 3
  • 28
  • 50
7
votes
4 answers

why interfaces in dynamic/loosely-typed languages?

I work in php, and the concept of interfaces seems to me a little useless here. From reading, I understand that interfaces are part of "design by contract", but without at least guaranteeing a return of a type of a particular kind, there really…
user151841
  • 17,377
  • 29
  • 109
  • 171
7
votes
1 answer

Code Contracts: How do I state in a post-condition that a field/property's value has not changed?

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." ); …
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
7
votes
2 answers

How might you implement design-by-contract in Clojure specifically or functional languages in general?

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…
Robert Campbell
  • 6,848
  • 12
  • 63
  • 93
7
votes
5 answers

How do I insert a precondition in a java class method or constructor?

This is for a java class I'm taking. The book mentions preconditions and postconditions but doesn't give any examples how to code them. It goes on to talk about asserts, I have that down, but the assignment I'm doing specifically states to insert…
Jay Rohrssen
  • 243
  • 1
  • 2
  • 9
6
votes
1 answer

Design by Contract for Objective-C?

Last year I took a class about the Design By Contract methodology. We programmed in C# with the Contract API that Mircosoft Research created, and generated unit tests with PEX. This year, I am programming iphone apps in xcode. I am wondering if…
mollerhoj
  • 1,298
  • 1
  • 10
  • 18
6
votes
3 answers

why using 'assert' in a project? (and why using it so many times)

i was reading through the sample code ListAdder, and there are many asserts right after the variable, or used in almost every method, for example : self.formatter = [[[NSNumberFormatter alloc] init] autorelease]; assert(self.formatter !=…
Paul
  • 6,108
  • 14
  • 72
  • 128
1 2
3
14 15