[Haskell-cafe] Object Oriented programming for Functional Programmers

Mike Meyer mwm at mired.org
Wed Jan 2 07:52:44 CET 2013

[Context destroyed by top posting.]
MigMit <miguelimo38 at yandex.ru> wrote:
>But really, "Design by Contract" — a theory? It certainly is a useful
>approach, but it doesn't seem to be a theory, not until we can actually
>prove something about it, and Eiffel doesn't seem to offer anything in
>this direction.

You just stated (briefly, and not very rigorously) the theory: DbC is a useful approach to programing. Note that it's a theory about *programming*, not the resulting program.

>And by "hack" I meant not the presence of pre/postconditions, but the
>fact that they don't affect anything else. Strip all of them away, and
>you'll have the program which is, essentially, the same (and, in fact,
>pre/postconditions are supposed to be removed in the final version of
>the program). Compare this to Haskell types, for example: an untyped
>version of Haskell won't be able to choose between two class instances,
>so, that would be an entirely different language.

Type classes are the wrong feature to look at. Type signatures are closer to what DbC is. Are type signatures a hack to get around deficiencies in the type inferencing engine? After all, you can strip all of them away and have essentially the same program.

Personally, I think the answer is "no", and for the same reason. We add type signatures to top level functions because it helps document the function, and to help isolate type errors during compilation. They makes *programming* easier, even if they don't change the program at all. Pre and Post conditions (and class invariants - they're also part of DbC!) serve pretty much the same function. They help document the classes and methods, and tools that generate class/method documentation from source always include them. They're as important as the type signature. They also help isolate bugs, in that you get told explicitly that routine foo passed in an invalid parameter to bar rather than an exception somewhere deep in the guts of bar that you have to work back from to find foo.

As I said before, I'm not sure I agree that the latter is worth the cost of using them for anything complex. The bugs they uncover are as likely to be in the pre/post conditions as in the code proper.  The documentation benefit is unquestionable, though. And if some condition is worth documenting, then having it as executable documentation means it gets tested with the rest of the code, so you know the documentation is correct. Which means that just adding conditions to a language misses most of the benefit of DbC. You need to fix the documentation system as well.

This is the kind of theory that you'll find in OOSC: why the features that are there help make programming easier. Not theories about how they make the resulting program better. Those two have a lot in common, though. Anything that makes witing correct code easier generally results in a better program.
Sent from my Android tablet with K-9 Mail. Please excuse my swyping.

More information about the Haskell-Cafe mailing list