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 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
this is nice.. considering formatMap value can be saved in config file or database. Its easier to scale.
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.
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
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. π
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 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
this is nice.. considering formatMap value can be saved in config file or database. Its easier to scale.
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.
Well one way or another the
formatAddressfunction needs to know the business rules. If I went with your approach I would pass the rules as an argument.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. π