Bypass duplicate repetitions with the same variable in if statements

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; } 
+4
source share
3 answers

Are you using Java 7? In this case, you can use the switch , which now supports String values:

 switch(installText) { case "Allow": installPermission = ENABLED; break; case "Disallow": installPermission = DISABLED; break; case "Default": installPermission = DEFAULT; break; default: installPermission = DEFAULT; break; } 
+2
source

Extract value verification into a separate method (I reuse part of Simeon Wisser's answer with improvements):

 public String getPermission(String permission) { String state = null; // If this is Java 7: /* switch(permission) { case "Allow": state = ENABLED; break; case "Disallow": state = DISABLED; break; case "Default": default: state = DEFAULT; break; } */ // If this is Java < 7: if (permission.equalsIgnoreCase("allow")) state = ENABLED; else if (permission.equalsIgnoreCase("disallow")) state = DISABLED; else state = DEFAULT; return state; } public void callingMethod(String permission) { installPermission = getPermissionState(permission); uninstallPermission = getPermissionState(permission); runPermission = getPermissionState(permission); } 

In addition, ENABLED , DISABLED and DEFAULT should simply be defined as constants in your class:

 public static final String ENABLED = "ENABLED"; public static final String DISABLED = "DISABLED"; public static final String DEFAULT = "DEFAULT"; 

However, I still do not understand your idea with reflection ...? I do not consider it necessary here if you do not have a precedent whose code you are not showing at the moment ...?

+1
source

if you are 100% sure that the string value will be one of these three. then you can do it as follows:

 Map<String,String> map; map.put("Default", "Default"); map.put("Allow", "Allow"); map.put("Disallow", "Disallow"); installPermission = map.get(installText); uninstallPermission = map.get(uninstallText); runPermission = map.get(runText); 

amuses

+1
source

All Articles