I believe that I am asking a slightly different question about the multi-year question of avoiding code repetition. The setup is pretty standard - there are many if statements that take similar actions. Below you will find a short example.
I am trying to find the best way to cope with situations such as in terms of code efficiency, compactness and simplicity of a programmer. Please note that the individual solutions related to each of these points are excellent, truly preferred, as I doubt that one solution applies to all three.
For example, one of the possible solutions that came to mind seems to be awkward and slow, especially on mobile devices like Android, plus it will only work when the variables were instance variables, and not just local (which can very it will be so). The idea is involved in an expression whose initialization expression will use the initialization twice spacer to populate the HashMap with lines corresponding to the variables involved (for example, the "installText" card in the "installPermission"), then for the loop it will contain one of If- -sese-if-else -if, which will use reflection to access variables by their names stored in hashmap.
How can I do it better? Thank you in advance for your time and advice!
if (installText.equals("Default")) { installPermission = DEFAULT; } else if (installText.equals("Allow")) { installPermission = ENABLED; } else if (installText.equals("Disallow")) { installPermission = DISABLED; } if (uninstallText.equals("Default")) { uninstallPermission = DEFAULT; } else if (uninstallText.equals("Allow")) { uninstallPermission = ENABLED; } else if (uninstallText.equals("Disallow")) { uninstallPermission = DISABLED; } if (runText.equals("Default")) { runPermission = DEFAULT; } else if (runText.equals("Allow")) { runPermission = ENABLED; } else if (runText.equals("Disallow")) { runPermission = DISABLED; }
source share