Skip to content

Instantly share code, notes, and snippets.

@sunnyy02
Last active December 10, 2022 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnyy02/f7636f10045894d4afd302332aff4b3c to your computer and use it in GitHub Desktop.
Save sunnyy02/f7636f10045894d4afd302332aff4b3c to your computer and use it in GitHub Desktop.
// many ifs
function performActions(actionName: string) {
if (actionName === "save") {
// save...
} else if (actionName === "update") {
// update ...
} else if (actionName === "delete") {
// delete...
}
}
// refactor with table driven methods
const funcs: Record<string, () => void> = {
save: () => {
// save ...
},
update: () => {
// update ...
},
delete: () => {
//delete ...
}
};
function performActions2(actionName: string) {
const action = funcs[actionName];
if (action) {
action();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment