4 ms·
The example in the article opens itself to an attack like this because it's simple (in an attempt at explaining things simply). It's easy to mistake the trivial
by lobster_johnson 9y ago
The example in the article opens itself to an attack like this because it's simple (in an attempt at explaining things simply). It's easy to mistake the trivialness for naivety.
But the "hell" part that monads solve really arise when you go further than this -- when you have error handling, null handling and asynchronicity and mutable state involved. For example, if each of these functions could return an Either or Maybe, your non-monadic, "if"-based version would get a lot hairier.
Monads abstract and generalize the concept of chaining computations. Of course you can write the chaining manually, but you would have to replicate the same logic for every chain of function calls, with all the ad-hoc error handling and so on in place. It may look "cleaner" to you, but there's no generalization happening, and monads exist to generalize patterns like these into a single tool that can used over and over again.
This is why every single monad code example in the article is identical.
- jstimpfle 9y agoYes, you can implement an "error monad" instance for each of a given set of procedures (returning Either, Maybe, or whatever). You can also wrap each of a given set of procedures that don't conform to a uniform interface (say, return null or < 0 on error) to be conformant. That's just basic hygiene. I don't see a problem. > Monads abstract and generalize the concept of chaining computations. I get that, but in the real world I don't find any need beyond sequencing (first do this, then do that) and the occasional nested-for or early return. Everything else is only giving up on other things that are extremely important, like modularity (complex types / protocols are inherently anti-modular) and deterministic resource usage. > This is why every single monad code example in the article is identical. I didn't even notice that, and I do not consider this a desirable goal at all. The examples do totally different things, and you want to see that. There's no need to swap one out for the other in any realistic scenario. Ever.
- dllthomas 9y ago> in the real world I don't find any need And I'm sure you're pretty productive with your choice of language, but bear in mind that tools shape our thinking. You don't find yourself reaching for those tools in part because you've never expected them to be there. That doesn't mean they can't be very useful.
- dmitriid 9y agoExactly. That’s why I would never write any of the ad-hoc examples the way they are written. And that’s why the article is criticized.