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

前端 未结 9 1175
傲寒
傲寒 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条回答
  •  旧时难觅i
    2020-12-12 21:16

    I think it is best to use both methods in conjunction rather than just one or the other.

    It has always seemed to me that fully enforcing a contract within the class and its methods themselves can be impractical.

    For example, if a function says it will hash a string by some method and return the hashed string as output, how does the function enforce that the string was hashed correctly? Hash it again and see if they match? Seems silly. Reverse the hash to see if you get the original? Not possible. Rather, you need a set of test cases to ensure that the function behaves correctly.

    On the other hand, if your particular implementation requires that your input data be of a certain size, then establishing a contract and enforcing it in your code seems like the best approach.

提交回复
热议问题