I'm a selftaught (web) developer. On sunny days, you can find me hiking through the Teutoburg Forest, on rainy days coding or with a good fiction novel in hand.
I find using nested hashmaps instead of if-else statements rather elegant when running decision based flows.
I've recently refactored a codebase with a lot of nested if-else control flows using this method. Said project is now much easier to read since decision logic is abstracted from the function using it.
What I also like is that it's easier to type your decision map with jsdoc or ts.
Example
constsequenceMap=Object.freeze({admin:{authorized:{action:()=>console.log("Logged in as admin")},notAuthorized:{action:()=>console.log("Could not login admin!")}},user:{authorized:{action:()=>console.log("Logged in as user")},notAuthorized:{action:()=>console.log("Could not login user!")}}});construnSequenceAction=(role,auth)=>{returnsequenceMap[role][auth].action();}runSequenceAction('admin','authorized');
Same functionality with if-else
functionrunAuthAction(role,auth){if(role==="admin"){if(auth==="authorized"){console.log("Logged in as admin");}elseif (auth==="unauthorized"){console.log("Could not login admin!")}}elseif (role==="user"){if(auth==="authorized"){console.log("Logged in as user");}elseif (auth==="unauthorized"){console.log("Could not login user")}}}
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 find using nested hashmaps instead of if-else statements rather elegant when running decision based flows.
I've recently refactored a codebase with a lot of nested if-else control flows using this method. Said project is now much easier to read since decision logic is abstracted from the function using it.
What I also like is that it's easier to type your decision map with jsdoc or ts.
Example
Same functionality with if-else