lundi 2 janvier 2017

How to optimize the following snippet?

The following snippet says the type based on name

public static List<String> LANGUAGES = {"js", "java", "html"};
public static List<String> LIBRARIES = {"jquery", "ember"};
public static List<String> BROWSERS = {"chrome", "safari", "mozilla"};
public static List<String> MAIL = {"gmail", "yahoo"};
public static List<String> EDITORS = {"vim", "vi", "notepad"};

public static getType(String name) {

String type = null;
if(LANGUAGES.contains(name)) {
  type = "languages";
} else if(LIBRARIES.contains(name)) {
  type = "libraries";
} else if(BROWSERS.contains(name)) {
  type = "browsers";
} Else if (MAIL. contains (name)) {
  type = "mail";
}else if(EDITORS.contains(name)) {
  type = "editors";
}
return type
}

Thought of trying the same with switch statement. But switch doesn't provide option to check with list.contains in Java.

Is there any way to write the above snippet in a better way? Because the getType method keeps on growing with the new types added.

Aucun commentaire:

Enregistrer un commentaire