DEV Community

Discussion on: 5 easy wins to refactor even the ugliest code

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

Hypothetical question: What happens if you add a new genre and you don’t want the default banner to be returned?

What I’m getting at is that it’s much safer to throw in a default. Or if it’s typescript it’s safer to return a never type. That way you can exhaustively consider enum values or any union of types. I describe that technique here.

Collapse
 
mlevkov profile image
Mikhail Levkovsky

Good point, for sure a switch statement should have a default.
I didn't include it in this case for brevity, and this is just an example. For something slightly more evolved I would actually resort to the factory pattern to create my return types and simplify the creation logic even more