Hacker News new | ask | show | jobs
by ogoparootbbo 1244 days ago
ad-hoc abstraction is the secret sauce that destroys code bases in the long run and makes things horrendously complex. Composition based on mathematics is more likely to survive years of serving as atomic elements than anything based on ad-hoc human intuition

Composition is hard

2 comments

Can you elaborate on this, or link to a more detailed description of what you mean?
I forgot where I read this, but I'll paraphrase: it's easier to work with 10 objects with 1000 applicable functions than with 1000 objects with 10 methods each.

In Haskell or other typed fp languages, you have all these well-recognized and well-used typeclasses and their attached methods, conceptually equivalent to generic interfaces in c#/java. It's easier to adapt new data structures into these interfaces and have all your existing functions immediately work with them.

The "mathematics" part here is that these typeclasses are backed by category theory that prescribes their behaviors.

There are plenty of random readings you can find, so I won't recommend them. Someone's bound to argue with me on what's a better entrypoint.

I do want to note that you don't need to use haskell or an fp language to use these concepts. Example: a complete set of fp data structures in typescript https://gcanti.github.io/fp-ts/modules/. It's just that fp languages make using them much easier because of their type system.

https://mitp-content-server.mit.edu/books/content/sectbyfn/b...

> The list, Lisp's native data structure, is largely responsible for such growth of utility. The simple structure and natural applicability of lists are reflected in functions that are amazingly nonidiosyncratic. In Pascal the plethora of declarable data structures induces a specialization within functions that inhibits and penalizes casual cooperation. It is better to have 100 functions operate on one data structure than to have 10 functions operate on 10 data structures. As a result the pyramid must stand unchanged for a millennium; the organism must evolve or perish.

I love programming with typeclasses based on mathematical abstractions. I guess it's because they actually have a chance of standing unchanged for a millennium.

But inheritance is harder, isn't it?
The concept of inheritance in programming is an ill conceived one. Better not to use it at all.
Inheritance is a concept of OOP not programming itself. And yes I much prefer composition.