Great post, I like how you took the time to explain step by step.
I'd suggest also the following change to make the "business rules" clearer by extracting them from the formatting logic:
const formatMap = { uk: ["zip", "suburb", "city", "state"], us: ["city", "state", "zip"], nz: ["zip", "city", "county", "state"], default: ["zip", "suburb", "state", "county"], } const formatAddress = (address) => { const format = formatMap[address.countryCode] || formatMap.default; return format.map(field => address[field]).filter(Boolean).join(', '); };
this is nice.. considering formatMap value can be saved in config file or database. Its easier to scale.
Well one way or another the formatAddress function needs to know the business rules. If I went with your approach I would pass the rules as an argument.
formatAddress
This is the final image in a magneto_perfection.jog meme. I love it.
this is also what i like to do whenever a switch case appears
Unless formatMap is something that needs to be dynamic, I suggest not to do this.
This mixes up type and data and also smells like premature abstraction.
Iβd never seen this pattern in JS, looks just as nice as in Java ππΌππΌ
Actually Iβve just written a post about this kind of pattern, if youβd like to take a look: dev.to/tomazlemos/keeping-your-cod...
Thanks for sharing this. π
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.
Great post, I like how you took the time to explain step by step.
I'd suggest also the following change to make the "business rules" clearer by extracting them from the formatting logic:
this is nice.. considering formatMap value can be saved in config file or database. Its easier to scale.
Well one way or another the
formatAddress
function needs to know the business rules. If I went with your approach I would pass the rules as an argument.This is the final image in a magneto_perfection.jog meme. I love it.
this is also what i like to do whenever a switch case appears
Unless formatMap is something that needs to be dynamic, I suggest not to do this.
This mixes up type and data and also smells like premature abstraction.
Iβd never seen this pattern in JS, looks just as nice as in Java ππΌππΌ
Actually Iβve just written a post about this kind of pattern, if youβd like to take a look:
dev.to/tomazlemos/keeping-your-cod...
Thanks for sharing this. π