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
131
votes
14 answers

Design by contract using assertions or exceptions?

When programming by contract a function or method first checks whether its preconditions are fulfilled, before starting to work on its responsibilities, right? The two most prominent ways to do these checks are by assert and by exception. assert…
andreas buykx
  • 12,608
  • 10
  • 62
  • 76
67
votes
18 answers

How much null checking is enough?

What are some guidelines for when it is not necessary to check for a null? A lot of the inherited code I've been working on as of late has null-checks ad nauseam. Null checks on trivial functions, null checks on API calls that state non-null…
James Schek
  • 17,844
  • 7
  • 51
  • 64
54
votes
5 answers

Using Design by Contract in Python

I am looking to start using DBC on a large number of Python-based projects at work and am wondering what experiences others have had with it. So far my research turned up the following: http://www.python.org/dev/peps/pep-0316/ - PEP 316 that is…
ipartola
  • 1,612
  • 3
  • 15
  • 25
53
votes
11 answers

How to show if a method may return null

After posting this question and reading that one I realized that it is very important to know if a method is supposed to return null, or if this is considered an error condition and an exceptions should be thrown. There also is a nice discussion…
Lena Schimmel
  • 7,203
  • 5
  • 43
  • 58
52
votes
7 answers

ReSharper - Possible Null Assignment when using Microsoft.Contracts

Is there any way to indicate to ReSharper that a null reference won't occur because of Design-by-Contract Requires checking? For example, the following code will raise the warning (Possible 'null' assignment to entity marked with 'NotNull'…
52
votes
8 answers

Unit tests - The benefit from unit tests with contract changes?

Recently I had an interesting discussion with a colleague about unit tests. We were discussing when maintaining unit tests became less productive, when your contracts change. Perhaps anyone can enlight me how to approach this problem. Let me…
Stefan Hendriks
  • 4,705
  • 5
  • 34
  • 43
48
votes
3 answers

Contract.Requires usage

Here is my problem. I am a very big fan of Design by contract, I am using this concept especially when developing libraries that can be used by other developers. I just found out a new way of doing this which is: Contract.Requires instead of…
GETah
  • 20,922
  • 7
  • 61
  • 103
47
votes
9 answers

Why is design-by-contract not so popular compared to test-driven development?

You may think this question is like this question asked on StackOverflow earlier. But I am trying to look at things differently. In TDD, we write tests that include different conditions, criteria, verification code. If a class passes all these tests…
Perpetualcoder
  • 13,501
  • 9
  • 64
  • 99
46
votes
10 answers

A good Design-by-Contract library for Java?

A few years ago, I did a survey of DbC packages for Java, and I wasn't wholly satisfied with any of them. Unfortunately I didn't keep good notes on my findings, and I assume things have changed. Would anybody care to compare and contrast different…
Chris Jones
  • 4,815
  • 6
  • 34
  • 28
40
votes
3 answers

What are contracts (as proposed for C++17)?

I read about contracts in Thoughts about C++17 by B. Stroustrup and assisted a small presentation talking about them but I am not sure I have understood them really. So I have some interrogations and if it is possible to illustrate them with some…
coincoin
  • 4,595
  • 3
  • 23
  • 47
39
votes
9 answers

'Design By Contract' in C#

I wanted to try a little design by contract in my latest C# application and wanted to have syntax akin to: public string Foo() { set { Assert.IsNotNull(value); Assert.IsTrue(value.Contains("bar")); _foo = value; …
IAmCodeMonkey
  • 1,576
  • 1
  • 11
  • 11
28
votes
8 answers

Design By Contract and Test-Driven Development

I'm working on improving our group's development process, and I'm considering how best to implement Design By Contract with Test-Driven Development. It seems the two techniques have a lot of overlap, and I was wondering if anyone had some insight…
Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
26
votes
10 answers

Does Design By Contract Work For You?

Do you use Design by Contract professionally? Is it something you have to do from the beginning of a project, or can you change gears and start to incorporate it into your software development lifecycle? What have you found to be the pros/cons of…
user290
25
votes
12 answers

Is Java assert broken?

While poking around the questions, I recently discovered the assert keyword in Java. At first, I was excited. Something useful I didn't already know! A more efficient way for me to check the validity of input parameters! Yay learning! But then I…
BlairHippo
  • 9,502
  • 10
  • 54
  • 78
21
votes
5 answers

Comparing design by contract to type systems

I recently read a paper that compared Design-by-Contract to Test-Driven-Development. There seems to be lot of overlap, some redundancy, and a little bit of synergy between the DbC and TDD. For example, there are systems for automatically generating…
aleator
  • 4,436
  • 20
  • 31
1
2 3
14 15