The code:
public String getTemperatureMessage(double temp)
{
if(temp < 32)
return "Freezing";
else if(temp < 60)
return "Brr";
else if(temp < 80)
return "Comfortable";
else
return "Too hot";
}
Regarding the code snippet above, the else ifs are technically superfluous and don’t change the behavior at all. However, I tend to like to put them in there to emphasize that the conditions are exclusive. What are your thoughts? Unnecessary or more clear?
Some would say that multiples return would be the problem here. But it’s not really my point.
For my point of view, the if/else if is really important, because even if in your case you return some value, removing the elses would mean that you wouldn’t put them anyway, and that would mean a totally different thing if the returns were not here.
Plus, imagine someday someone want to edit your code, and clean it up for a single return, this person could misunderstand your code and do a grave mistake like this :
To clarify my point of view, keep the elses, it keep your code clear.