Consider the scenario:
List<String> weekdays = Arrays.asList("sun", "mon", "tue");
long sunTemp = 0;
long monTemp = 0;
for(String day : weekdays){
if(day.equals("sun"){
sunTemp = getSunTemp();
}else if(day.equals("mon")){
monTemp = getMonTemp();
// use sunTemp ex: totalTemp = sunTemp + monTemp
}
//...more code
}
What, if anything is wrong with the above?
The following stand out as awkward to me:
- usage of the for loop
- assigning a value in the 'if block' and use it in the 'else block'
the next iteration.
- Code depending on the pre-knowledge of data
(order and exact values the list.)
Can someone put in formal terms what is correct or wrong with the above?
Thanks much!