Questions tagged [design-by-contract]

Design by contract (DbC) 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.

Design by contract (DbC), also known as contract programming, programming by contract and design-by-contract programming, is an approach for designing 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.

30 questions
55
votes
10 answers

Should a method validate its parameters?

Say you are designing a Square root method sqrt. Do you prefer to validate the parameter passed is not a negative number or do you leave it up to the caller to make sure the param passed is valid. How does your answer vary if the method/API is for…
Amit Wadhwa
  • 1,962
  • 1
  • 15
  • 20
41
votes
9 answers

Why is there such limited support for Design by Contract in most modern programming languages?

I recently discovered Design by Contract (DbC) and I find it an extremely interesting way to write code. Among other things, it would seem to offer: Better documentation. Since the contract is the documentation, it's impossible for one to be out of…
Ceasar
  • 559
  • 4
  • 9
9
votes
3 answers

Checking preconditions or not

I've been wanting to find a solid answer to the question of whether or not to have runtime checks to validate input for the purposes of ensuring a client has stuck to their end of the agreement in design by contract. For example, consider a simple…
void.pointer
  • 4,983
  • 8
  • 30
  • 40
6
votes
6 answers

Should I assert the preconditions of functions in a public API?

I am writing a library for some data structures in C that will be used in embedded systems. I have had issues designing and coming up with a solid error handling plan. This API is only subject to logic errors which is why I am so conflicted. By this…
6
votes
1 answer

Advantages of using the library PyContracts over assert statements

Today I stumbled upon the python package called PyContracts. However, python has the assert statement which seems to allow you to do exactly those things. What advantages do contracts have over assert statements?
chtenb
  • 341
  • 1
  • 2
  • 13
6
votes
2 answers

How are design-by-contract and property-based testing (QuickCheck) related?

Is their only similarity the fact that they are not xUnit (or more precisely, not based on enumerating specific test cases), or is it deeper than that? Property-based testing (using QuickCheck, ScalaCheck, etc) seem well-suited to a functional…
Todd Owen
  • 173
  • 5
6
votes
2 answers

Options for programming by contract in Java

I am working on a project (which includes JavaEE web apps and JavaSE apps) that has grown from a single developer to a team of three, and issues of readability and robustness are starting to emerge. One glaring omission from the Java language has…
Phyxx
  • 169
  • 3
5
votes
1 answer

Detailed Scope of Work.. Waterfall?

I have a client that's requested a detailed Scope of Work/Statement of Work. Upon looking into it, it seems they want timelines, costs, features, the whole nine. In order to do a detailed SOW, one basically has to have the whole system planned out…
jleach
  • 2,632
  • 9
  • 27
5
votes
3 answers

What is the difference between dependent typing and contracts?

What is the difference between dependent typed languages and languages like Spec# and Eiffel that allow you to specify "contracts" for functions in your code for pre/postconditions? Is dependent typing basically the purely functional version of…
5
votes
2 answers

Earliest use of Comments as Semantically Meaningful Things in a Program?

In certain corners of the PHP meta-programming world, it's become fashionable to use PHPDoc comments as a mechanism for providing semantically meaningful information to a program. That is, other code will parse the doc blocks and do something…
Alana Storm
  • 301
  • 1
  • 7
4
votes
4 answers

Is design by contract useful without unit testing?

I read Bertrand Meyer's paper on design by contract yesterday and it is not very clear for me what is the relationship between DbC and testing, since it appears that without testing I cannot be certain all the assertions were hit at some…
edalorzo
  • 2,634
  • 1
  • 19
  • 28
4
votes
3 answers

Concept to validate objects across languages?

An interesting question I've stumbled upon: Let's assume a java application creates a data model, converts this data to a json object with two fields and uploads it to a server: { "FirstName": "Foo", "LastName": "Bar" } Now a different…
Samuel
  • 723
  • 1
  • 5
  • 7
4
votes
3 answers

How to loosen input contracts by inheritance?

According to LSP wiki: Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e. an object of type T may be…
inf3rno
  • 1,209
  • 10
  • 26
4
votes
1 answer

Is Design by Contract the same as using Interfaces to create a "contract"?

Does an OOP design that uses a Design by Contract mean the designer is using interfaces to create a "contract." The term "contract" is used quite often when discussing OOP interfaces, so I didn't know if these were talking about the same concept…
johnny
  • 3,669
  • 3
  • 21
  • 35
4
votes
3 answers

In design by contract, why preconditions should be ensured by a client and postconditions - by a supplier?

I'd heard about Design by Contract a long time ago and always was confused by this question. The approach uses real-world client-supplier analogy to describe caller-callee relationships. It stays, that if a client ensures preconditions before…
neoascetic
  • 143
  • 4
1
2