DEV Community

Discussion on: Probably the hottest code refactoring you ever saw 🔥

Collapse
 
muramasah profile image
Felipe

Hi Ioannis, I think you cleaned very well the code, but it is still rigid an fragile. For example, try to change the way the message is formated in some countries, like using dots for some properties instead of comas, or try to add a new field to every country. In every case you must change the written code breaking with the SOLID's open-closed principle.

The source of evil here is the switch, your function formatAddress has two responsabilities, (1) to determine what country code has the adress object, and (2) choose the properties to be passed to joinStrings for each case.

An easy pattern that bring us some flexibility in these cases is the chain of responsabilities. If you dislike this approach you have others more functional but the responsabilities coupled there are a big issue from my point of view, because limits the extention of features that has a lot of probabilities to change.

Btw, all of I'm saying here do not take away the fact that you have done an awesome job until here!

Sorry for the bad english,
Have fun!

Collapse
 
potouridisio profile image
Ioannis Potouridis

You're totally correct.

The format and number of countries we serve is fixed so I didn't take into consideration these principles. I should but this was a function with a specific purpose and posted this as an example for cleaner code. Thank you for your feedback and don't worry for your English. 😀