My code is something like this :
if(country == china)
{
getCNData();
}
else {
getDefaultDataForallCountries();
}
Now I need to add similar logic as CN for some other country say US . The option available to me is to add one more country check in if condition and make it like
if(country ==china && country==US){
getCNandUSData();
}
else {
getDefaultDataForallCountries();
}.
1) I am somehow not comfortable to go for this solution as this is not generic . What if tomorrow I need to have same CN logic applied to another country say france . Can you please suggest me how can I make my code better and more generic.
2) Also I am not comfortable with the naming conventions. If say I go with my approach of adding US in If condition , Should I refactor the class name and function name to getCNAndUSData () ?
I am not sure here of what is the correct way to deal with such existing code.
Appreciate your comments.
This type of issue (overuse of “if” and “switch” statements) is neatly handled by implementing a strategy pattern with a abstract factory. Basically you want to change the algorithm without changing your implementation and duplicating the code over and over and over.
Enjoy!