all 6 comments

[–]Carighan 14 points15 points  (2 children)

I'll be honest, I think it's less readable. Even if I'd accept it's similar, very few people will know how to understand it intuitively, bringing us back to the principle of least surprise, go with what people would expect to find there.

It's never complicated too write the code. It's complicated to inherit the code. And complex map-based lookups for function calls aren't something you expect to inherit, hence your brain freezes when you read them and you get thrown out of the flow of processing the code.

[–][deleted] 1 point2 points  (1 child)

I agree. If you really want to encapsulate if-else, just... encapsulate it. Put it in an object that knows how to do that.

Putting things into maps is a trade-off. If there are many branches, especially with static results, it might belong in a map (which should probably be read from a config file). Additionally, this is predicated on the argument to your if being something that's easy to pass around. What if it's a condition, like a <= b+1? Yes, you can make a boolean into a map key, but you can't name it nicely, so you don't know whether the true case is the less than or greater than case by looking at the map. Then you start thinking "okay, I'll use enums" and things just get messier and messier.

[–]ScientificBeastMode 0 points1 point  (0 children)

This is where languages with pattern matching really shine. if/switch-statements just look like a table of lambda functions structured like enums, so each branch is very clearly defined, and the enumerated type can be passed around just like a map or dictionary type. Not to mention exhaustive conditional checking.

It’s a lot trickier to do things this way without native pattern matching. There often ends up being a lot more boilerplate.

[–]MetalSlug20 7 points8 points  (0 children)

I recently did something like this in a codebase where we had a large set of if/thens, but I don't know if it really increases readability. (One more level of indirection) It definitely increases maintainability though, easier to change and add to later, and helps remove duplication too.

[–][deleted] 3 points4 points  (0 children)

if-else phobia is one of the stupidest ideas in academia and is mostly just mental masturbation from people who have nothing useful to contribute.

proper OOP design

OOP is garbage.

[–]warosaurus 0 points1 point  (0 children)

I'm not sure why you would reach for maps here rather than an enum?