DEV Community

Discussion on: It's working, why change it? - Code Review Chronicles

Collapse
 
lampewebdev profile image
Michael "lampe" Lazarski

Code readability should always come before one-liners.

In general, one-liners are something that developers think is cool.
And yes to show-off they are but for maintainability, they are usually the worst kind of code.
Why?
1) Hard to debug
2) You need to much time to understand if after not seeing if for 2 weeks
3) They usually don't add any performance
4) Writing tests can be hard.

I don't get why devs hate switches?
So it is better to write a bunch of if's?

Languages are like a toolbox. Use the right tool for the right job!

Collapse
 
dvddpl profile image
Davide de Paolis

agree.
switches are not bad. and are better then ifs
but it's better to have maps/objects than switches because they can grow more organically without much changes in the code, avoid issues like fallthrough and missing returns/break, better performance (you access directly the value by name/prop not by comparing to all the cases) and overall readability.