I wonder if this is a good way to refactor multiple if statements? What I have heard is that if-statements are "bad" practice and i would like to take my programing to the next level by refactor some of my code.
Original code:
public int Colors(String col, int row){
int ret = 0;
if(col.equals("R")){
ret = Color.parseColor("#EF5350");
}else if(col.equals("B")) {
ret = Color.parseColor("#7986CB");
}else if(col.equals("V")){
ret = Color.WHITE;
}else if(col.equals("G")){
ret = Color.parseColor("#FFE082");
}else if(Math.floorMod(row,2) == 0){
ret = Color.parseColor("#e0e0e0");
}else{
ret = Color.parseColor("#eeeeee");
}
return ret; }
New code:
public int Colors(String col, int row){
Map<String,Integer> ColorMap1 = new HashMap<>();
ColorMap1.put("R", Color.parseColor("#EF5350"));
ColorMap1.put("B",Color.parseColor("#7986CB"));
ColorMap1.put("V",Color.parseColor("#FFE082"));
ColorMap1.put("G",Color.parseColor("#FFFFFF"));
Integer current = ColorMap1.get(col);
Map<Integer,Integer> ColorMap2 = new HashMap<>();
ColorMap2.put(0,Color.parseColor("#e0e0e0"));
ColorMap2.put(1,Color.parseColor("#eeeeee"));
Integer current2 = ColorMap2.get(Math.floorMod(row,2));
return current != null ? current : current2 ;
}