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
4
votes
4 answers

Do you have any tips to effectively use Java Assert?

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?
Mohan Narayanaswamy
  • 2,149
  • 6
  • 33
  • 40
4
votes
2 answers

IOC Container Handling State Params in Non-Default Constructor

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…
4
votes
1 answer

Differences between Code Contract and Spec#

I want to implement the DBC in C#. I faced with the Spec# and Code Contract for it. What is the difference between Spec# and Code Contract?
tvahid
  • 210
  • 1
  • 9
4
votes
1 answer

F# argument checking

F# has some nice succint argument checking functions that can be used like this: let foo (bar : string) : string = if bar = null then nullArg "bar" ... I prefer a more prescriptive expression, however, a la Code Contracts: let foo…
Bent Rasmussen
  • 5,538
  • 9
  • 44
  • 63
4
votes
3 answers

Optional PHP type hint/check for unit testing or static analysis?

The PHP type hints does not support scalar variable [1] such as int or string However, we found it is still very useful to annotate the type (int or string) in our function during continuos integration to spot bugs, e.g. Currently I use method…
Howard
  • 19,215
  • 35
  • 112
  • 184
4
votes
2 answers

Should I use both NotNull and ContractAnnotation("null => halt")?

It seems that NotNull and ContractAnnotation("key: null => halt") are fairly similar in their effect on R#. Is there something that I am missing? Should I always apply both?
Den
  • 1,827
  • 3
  • 25
  • 46
4
votes
3 answers

Design by contract and class invariant

I'm reading about dbc (http://en.wikipedia.org/wiki/Design_by_contract) Can someone please give me a simple example of using class invariants in relation to inheritance?
loudiyimo
  • 1,177
  • 2
  • 12
  • 11
4
votes
1 answer

How can class invariant strengthen pre and post-conditions?

Link You can think of the class invariant as a health criterion, which must be fulfilled by all objects in between operations. As a precondition of every public operation of the class, it can therefore be assumed that the class invariant…
4
votes
5 answers

The best way to assert pre-condition and post-condition of arguments and values in .NET?

I have been thinking about design by contract lately and I was wondering what people think is the best way to assert pre-condition and post-condition of values in .NET? i.e. validating argument values to a method. Some people recommend Debug.Assert…
James Newton-King
  • 48,174
  • 24
  • 109
  • 130
4
votes
1 answer

Why design by contract isn't more popular?

In theory, it seems like a great solution for avoiding bugs, but why in practice we hear so little about it? Why can't we see more support for it on Java or .net for example?
Kico Lobo
  • 4,374
  • 4
  • 35
  • 48
4
votes
3 answers

Are preconditions and postconditions needed in addition to invariants in member functions if doing design by contract?

I understand that in the DbC method, preconditions and postconditions are attached to a function. What I'm wondering is if that applies to member functions as well. For instance, assuming I use invariants at the beginning at end of each public…
3
votes
1 answer

How to code by contract using RESTful Web Services

Our company as a best practice creates Web Services using a Contract-First approach. The WSDL is defined before the implementation is considered. Is it possible to use such an approach using REST? I was thinking of using an XSD however calls are not…
Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
3
votes
2 answers

Can this statement be regarded as a class invariant?

This is a highly general thought, but let's use C# in this example. Given that I have a disposable class Foo, i.e., it implements IDisposable. Foo has a boolean flag disposed that is false until Dispose is called, after which it's true. All public…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
3
votes
1 answer

I need a basic concrete example on how to use TDD along with Design by Contract

I have seen many questions like this and this. Some people see there is overlapping between TDD and Design by Contract and others say they are complementary, I am biased to the second one, so I need a very basic, correct and complete example in any…
Drifter
  • 61
  • 4
3
votes
1 answer

Racket: contracts on higher-order functions

I am using Racket contract system, and I want to export a function of no arguments, which returns a lambda expression with no arguments, e. g.: #lang racket (define (foo) (do-somthing) (lambda () (do-other things))) Does anyone know how to…
user618815