DEV Community

Discussion on: Keeping your code clean by sweeping out "if" statements

Collapse
 
tomazfernandes profile image
Tomaz Lemos

Hi Rasmus! Thanks for bringing in your perspective. Your point is a valid one, in that the map version is a lot less flexible than using ifs. But I was reflecting on it and realised that it's exactly that what I like the most about it, and what frightens me more about those uncontrolled ifs.

I think that good code implies in enforcing constraints, and making intentions and interfaces clear, and it's something hard to achieve when solving everything with if statements.

To use your example, if you have lots of chained if statements and the one before the last is a little different due to an enhanced condition, or has a side effect the others don't, it can be really tricky to find it.

When you enforce the constraints, with a Map for example, you're saying that that's all there is about that logic, that's the contract.

Should the requirements change, perhaps that contract isn't useful anymore, and then you should have to design a new one. Or at least you would have to make it clear that that one condition is different than the others.

Coding for me is all about making intentions clear and stating what the business rules are, and I think this approach helps me in achieving it.

Thank you very much again for your perspective and a happy new year to you as well!