Eschew ELSE
dobbscodetalk.com
dobbscodetalk.com
Reading that prompted a major change in my coding style, and in my opinion, a major improvement in readability.
As you know, there is a cost to adding functions. They are not free from a code performance standpoint. Are they so close to being free that you don't need to worry? That depends on the processor and the application. I still do embedded and assembly programming, so I can't afford to drop this habit, because there, it does matter. So for web apps, I continue to use them liberally, but definitely not "at the drop of a hat".
Sure, a COND that's the only (or final) form in the function that contains it has the same property, but the key thing there is that it's at the end, not that it's a COND. COND really is the same thing as if ... else if ... else ...; using that rather than IF when you have more than two branches is a good thing, for sure, but I don't see how it's good for the reason you give.
Am I missing something?
ELSE statements don't kill. Drunk programmers kill.
This reminds me of something I once ran into:
Junior Programmer:
if (m==1){Month="January")}
if (m==2){Month="February")}
if (m==3){Month="March")}
if (m==4){Month="April")}
if (m==5){Month="May")}
if (m==6){Month="June")}
if (m==7){Month="July")}
if (m==8){Month="August")}
if (m==9){Month="September")}
if (m==10){Month="October")}
if (m==11){Month="November")}
if (m==12){Month="December")}
Senior Programmer: switch(m)
{
case 1:
Month = "January"
break;
case 2:
Month = "February"
break;
case 3:
Month = "March"
break;
case 4:
Month = "April"
break;
case 5:
Month = "May"
break;
case 6:
Month = "June"
break;
case 7:
Month = "July"
break;
case 8:
Month = "August"
break;
case 9:
Month = "September"
break;
case 10:
Month = "October"
break;
case 11:
Month = "November"
break;
case 12:
Month = "December"
break;
default:
Month = "unknown"
}
Lazy Programmer: MonthNames == ["","January","February","March",...]
Month = MonthNames[m]The code is much more compact and much easier to read. (Of course you'd do bounds checking on 'm' before the assignment, but that's not a huge burden.)