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
20
votes
2 answers

Design by Contract in Swift

Does Swift provide a native Design by Contract support? I understand that it can be done during runtime through assertions, but could it be done during compile time? Or, are there any external plugins/libraries that do this? EDIT By saying "during…
avismara
  • 5,141
  • 2
  • 32
  • 56
20
votes
9 answers

Do preconditions ALWAYS have to be checked?

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…
Pin
  • 3,746
  • 4
  • 26
  • 33
20
votes
6 answers

Library to facilitate the use of the "design by contract" principle

Is there any library that aids in implementing the design by contract principle in a C++ application? In particular, I'm looking for a library that facilities the usage of the principle, something like this.
yesraaj
  • 46,370
  • 69
  • 194
  • 251
19
votes
8 answers

Ruby and duck typing: design by contract impossible?

Method signature in Java: public List getFilesIn(List directories) similar one in ruby def get_files_in(directories) In the case of Java, the type system gives me information about what the method expects and delivers. In Ruby's…
davetron5000
  • 24,123
  • 11
  • 70
  • 98
18
votes
1 answer

Common Lisp idioms for argument checking and other paranoia?

This question is about coding conventions, best practices, and style in production, mission-critical Common-Lisp code. I perused Google's Common-Lisp Style Guide, but didn't find anything clearly addressing my specific concern, which I express by…
Reb.Cabin
  • 5,426
  • 3
  • 35
  • 64
17
votes
1 answer

Am I implementing this simple contract incorrectly?

This is my code: public class RegularPolygon { public int VertexCount; public double SideLength; public RegularPolygon(int vertexCount, double sideLength) { Contract.Requires(vertexCount >= 3); VertexCount =…
Nobody
  • 4,731
  • 7
  • 36
  • 65
16
votes
6 answers

checking invariants in C++

Are there any established patterns for checking class invariants in C++? Ideally, the invariants would be automatically checked at the beginning and at the end of each public member function. As far as I know, C with classes provided special before…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
15
votes
4 answers

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 for "code contracts php" gave nothing to me. Note: by "code…
zerkms
  • 249,484
  • 69
  • 436
  • 539
14
votes
1 answer

Code Contracts Vs. Object Initializers (.net 4.0)

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…
Brent Arias
  • 29,277
  • 40
  • 133
  • 234
14
votes
4 answers

Java: clean way to automatically throw UnsupportedOperationException when calling hashCode() and equals()?

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,…
SyntaxT3rr0r
  • 27,745
  • 21
  • 87
  • 120
12
votes
4 answers

How do you do Design by Contract in Perl?

I'm investigating using DbC in our Perl projects, and I'm trying to find the best way to verify contracts in the source (e.g. checking pre/post conditions, invariants, etc.) Class::Contract was written by Damian Conway and is now maintained by C.…
Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
12
votes
2 answers

Code Contracts: Why are some invariants not considered outside the class?

Consider this immutable type: public class Settings { public string Path { get; private set; } [ContractInvariantMethod] private void ObjectInvariants() { Contract.Invariant(Path != null); } public Settings(string…
Romain Verdier
  • 12,833
  • 7
  • 57
  • 77
11
votes
2 answers

How does .NET 4.0's design by contract compare to Eiffel?

I had the "pleasure" to be taught Eiffel at college by none other than Bertrand Meyer himself and just read that .NET 4.0 will include design by contract. Can anyone with some insight elaborate on how powerful this will be compared to Eiffel's…
kitsune
  • 11,516
  • 13
  • 57
  • 78
10
votes
9 answers

How can I show that a method will never return null (Design by contract) in C#

I have a method which never returns a null object. I want to make it clear so that users of my API don't have to write code like this: if(Getxyz() != null) { // do stuff } How can I show this intent?
StackUnderflow
  • 24,080
  • 14
  • 54
  • 77
10
votes
5 answers

Design by contracts and constructors

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…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
1
2
3
14 15