jaysome said:
This is equivalent to:
if ( Mode == 2 )
{
Result = TwoWayFunc(Param1, Param2, Param3);
}
else
{
Result = FourWayFunc(Param1, Param2, Param3);
}
Hey, that looks like a good job for a conditional expression!
result = Mode == 2 ? TwoWayFunc(Param1, Param2, Param3) :
FourWayFunc(Param1, Param2, Param3);
I presume nobody's scratching their head here. Whether you do that with an
if-statement or a conditional depends on how comfortable you are with
stuffing things on one line. We're assuming, of course, that "Param1" and
friends really are called that, and are not placeholders for two mile long
expressions with side effects.
But hold on, is "2" a magic value here? Wouldn't a variable called "Mode"
hold some sort of state parameter? Well then
switch (Mode) {
case 2: TwoWayFunc(Param1, Param2, Param3); break;
default: FourWayFunc(Param1, Param2, Param3); break;
}
is a nice way of emphasizing this. Now, "2" shouldn't be a magic value, so
toss in a #define or make Mode of enum type, but that's changing around
semantics.
The original "really good programmer" bit off more than he could chew,
from the standpont if understandability and maintainability, as the
equivalent code should not cause your head to scratch at all.
In the world of safety critical software, which I deal with on a
regular basis, we have no tolerance for such code as that written by
the "really good programmer". He or she either changes it or he or she
is out the door. No if, ands, or but about it.
Must be nice to be able to cull the herd like that. I have to take the
occasional clue-by-four to my colleagues when I get called in to fix a
problem that's ultimately the result of bozotic code... admittedly not in C,
I don't think they're ready for that. ("What's wrong with an empty exception
handler? I really want to ignore errors here!")
S.