Hey guys, take a look at this:
switch(LCSrandom(4))
{
case 0:
addstrl("You keep the gas floored!", gamelog);
gamelog.newline(); //Newline.
break;
case 1:
addstr("You swerve around the next corner!", gamelog);
gamelog.newline(); //New line.
break;
case 2:
addstr("You screech through an empty lot to the next street!", gamelog);
gamelog.newline(); //New line.
break;
case 3:
if(yourworst>15)
addstr("You boldly weave through oncoming traffic!", gamelog);
gamelog.newline(); //new line.
else
addstr("You make obscene gestures at the pursuers!", gamelog);
gamelog.newline(); //Another new line.
break;
}
In cases like these, I'd like to do the following:
1. Define a third overloaded addstr:
int addstr(const char *text, Log &log, bool newline)
{
gamelog.record(text);
if(newline) gamelog.newline();
return addstr(text);
}
2. And modify the switch statement to be the following:
switch(LCSrandom(4))
{
case 0:
addstr("You keep the gas floored!", gamelog, true);
break;
case 1:
addstr("You swerve around the next corner!", gamelog, true);
break;
case 2:
addstr("You screech through an empty lot to the next street!", gamelog, true);
break;
case 3:
if(yourworst>15)
addstr("You boldly weave through oncoming traffic!", gamelog, true);
else
addstr("You make obscene gestures at the pursuers!", gamelog, true);
break;
}
I've never been a fan of switch statements, but when I do use them, I find short cases better looking.
Or, as an alternative:
1. Define a new addstr wrapper function by the name of addstrl or addstrn or something else that is logical and easy to type.
int addstrl(const char *text, Log &log)
{
gamelog.record(text);
gamelog.newline();
return addstr(text);
}
2. And modify the switch statement to be the following:
switch(LCSrandom(4))
{
case 0:
addstr("You keep the gas floored!", gamelog);
gamelog.newline(); //Newline.
break;
case 1:
addstrl("You swerve around the next corner!", gamelog);
break;
case 2:
addstrl("You screech through an empty lot to the next street!", gamelog);
break;
case 3:
if(yourworst>15)
addstrl("You boldly weave through oncoming traffic!", gamelog);
else
addstrl("You make obscene gestures at the pursuers!", gamelog);
break;
}
That does the following:
A) Gets rid of the redundant if statement
B) Get rid of the redundant third parameter
C) Change the name of the function. That's not always the best method, but in this case, the only effected code is that which wants newlines automatically appended in the log without having to write a separate newline() call.
In the normal code, I find the first option (the overloaded function) to be more readable and nicer, but in switch statements such as this one, I find the second option (the new, differently named function) to be more readable and nicer. [Edit: Thinking again, I find the alternate option nicer in both cases. Every time I need a newline for an addstr, I could just use addstrl("message", gamelog); rather than addstr("message", gamelog, true); or addstr("message", gamelog);gamelog.newline();] [Edit2: And another reason I forgot to mention; it's easier to type addstrl("message", gamelog); rather than the other two alternatives
]
What do you guys think?
I can go either way.