DEV Community

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

Collapse
 
mfp22 profile image
Mike Pearson

I love this post. This is an underappreciated application of DRY.

"if" blocks can become magnets for repeated code that often increases unnecessary coupling to APIs---all kinds of APIs. One example is when you have an if-else statement that both contain the same function call but pass in a different parameter. Instead you can use a ternary to make a simple conditional assignment and then a single function call. It's easier to follow the logic, and if you decide to refactor to a pure function, you only need to change one line to a "return" statement.