6 ms·
I have never seen any reason to consider if statements to be evil enough that I should absolutely avoid them in my code. AFAIK, this is one of those pointless m
by chaosprophet 17y ago
I have never seen any reason to consider if statements to be evil enough that I should absolutely avoid them in my code. AFAIK, this is one of those pointless movements.
If anybody can explain why ifs are supposed to be so horrible, then perhaps I might change my mind.
- amalcon 17y agoIt looks like the complaint is against using IF when type polymorphism would be more appropriate (usually of the form "if(x instanceof a)"). It is almost never a good idea to use IF in those cases, but frankly, this website does not do a good job of explaining the problem.
- chaosprophet 17y agoIsn't that a minimal use-case??? The presentation of the site seems to suggest that any and all IF statements are to be avoided (though i have to admitted I did'nt get past the landing page).
- mdemare 17y agoAnd even in those cases, it isn't cut-and-dried. They claim "The advantage is that tomorrow, we can fulfill the request for a new type of bond by simply creating a new class, with the single required logic to calculate the value of that new bond." Simply? I'd say that creating a new class is a hell of a lot more work than creating a new elseif branch.
- j_baker 17y agoThat depends on the context of course. What if that elseif branch needs to be created in two places? Or 20 places?
- loup-vaillant 17y agoTo replace `if(x instanceof a)`, you don't need type polymorphism (way too heavy). You need algebraic datatypes. With pattern matching (a glorified `switch`), you have the best of both worlds: the compiler make sure you cover all cases (and warn you if you don't), and the code is still local (you don't have to fetch several files to assess its behaviour). If you want to add one case, you still have to modify several snippets, but again, the compiler will warn you if you forget one. Of course, if their language don't have algebraic datatypes, I strongly suggest they learn one that have it.
- mquander 17y agoAlthough it's not what the site is talking about, I think it's often a lot more readable when many "if"s are replaced with "switch" statements or their big brother, ML-style pattern matching. Those make it more clear when conditions are mutually exclusive and help eliminate the error-prone simplification of boolean conditions in your head.