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

前端 未结 9 1161
傲寒
傲寒 2020-12-12 21:00

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

9条回答
  •  伪装坚强ぢ
    2020-12-12 21:18

    Design-by-contract and test-driven development are not mutually exclusive.

    Bertrand Meyer's book Object Oriented Software Construction, 2nd Edition doesn't say that you never make mistakes. Indeed, if you look at the chapter "When the contract is broken", it discusses what happens when a function fails to accomplish what its contract states.

    The simple fact that you use the DbC technique doesn't make your code correct. Design-by-contract establishes well-defined rules for your code and its users, in the form of contracts. It's helpful, but you can always mess things up anyway, only that you'll probably notice earlier.

    Test-driven development will check, from the outside world, black box style, that the public interface of your class behaves correctly.

提交回复
热议问题