Hacker News new | ask | show | jobs
by Toutouxc 53 days ago
One thing I noticed is that the principle (better demonstrated in the linked matklad article) seems to make sense at the "a bunch of related functions" level, but it also goes against one of the main principles of OOP, which is not to prescribe behaviors to others.

You don't want to go "woof if you're a dog, meow if you're a cat", you want to go "make a sound, whoever you are". If you're a fish, you can no-op or not "accept" the call, depending on the language and convention. Obviously there is a lot of nuance, but the general idea is to push responsibilities and logic (which includes branching) down, to the level/entity/object that's best equipped to handle it.

If you apply this to the "a bunch of functions" example, you get low-level functions with branching, but you also get clean callers.

I feel like this isn't such a huge problem IRL, it's usually quite apparent where a decision should be made, but both of these "schools of thought" seem equally valid to me.

1 comments

But OOP’s insistence on dispatching "at the leaves" is mostly understood as misguided or overly dogmatic these days. In particular, tagged unions and pattern matching are incredibly useful tools that go right against OO dogma. And they’ve always been so, since their introduction in the early 70s. The entire idea of "objects combine behavior and data" is problematic in many ways, not least because it can be very suboptimal on modern hardware compared to data-oriented approaches.
I strongly feel that pattern matching is the most useful language feature that there is for writing code that's correct and easy to understand. Once you've used a language that has it, you never really want to go without it again.