2 ms·
> Prefer polymorphism to “if/else” and “switch” - if anything, that makes code less readable, as it hides the dispatch targets. Worse, I think, is that it hide
by kps 4y ago
> Prefer polymorphism to “if/else” and “switch” - if anything, that makes code less readable, as it hides the dispatch targets.
Worse, I think, is that it hides the condition, which can be arbitrarily far away in space and time.
Certainly dynamic dispatch can be very useful, and the abstraction can be clearer than the alternatives. A rule of thumb is to consider whether you'd do it if you were writing in C, using explicit tables of function pointers. If that would be clearer than conditional statements, do it.
(In case it isn't obvious, I'm talking about languages in the C++/Java vein here.)