I only use switch statements in two scenarios:
switch
A: I'm using an Abstract Factory for something.
public class PokemonFactory { public static createPokemon (type: PokemonType): Pokemon { switch (type) { case 'pikachu': return new Pikachu(); case 'charizard': return new Charizard(); case 'squirtle': return new Squirtle(); default: return null; } } }
B: If I'm writing reducers in redux
switch (action) { case actions.FETCHING_PROFILE: case actions.FETCHING_PROFILE_SUCCESS: case actions.FETCHING_PROFILE_FAILURE: case actions.SUBMITTING_PROFILE: case actions.SUBMITTING_PROFILE_SUCCESS: case actions.SUBMITTING_PROFILE_FAILURE: return { ...state, profile: profileReducer(state.profile, action); default: return state; }
Imagine the OR pipes if that last one was made using if statements. Being able to match many cases at one time saves so much typing.
OR
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I only use
switch
statements in two scenarios:A: I'm using an Abstract Factory for something.
B: If I'm writing reducers in redux
Imagine the
OR
pipes if that last one was made using if statements. Being able to match many cases at one time saves so much typing.