Best way to write a function to do something like this
I am not totally familiar with all the different syntax constructs for doing something like this - I started with the SWITCH version
Code:
bool isPunc(int &cc) {
switch (cc) {
case cDash:
return true;
break;
case cPeriod:
return true;
break;
case cSpace:
return true;
break;
default:
return false;
break;
}
}
First question - those BREAK; statements aren't really needed - or maybe it just isn't a good practice to do RETURN's from CASE statements?
At any rate - I guess I could have just declare a BOOL and set it as TRUE in each CASE - then simply return that BOOL...
But then I thought of doing it this way.
Code:
bool isPunc(int &cc) {
if (cc == cDash) {
return true;
} else if (cc == cPeriod) {
return true;
} else if (cc == cSpace) {
return true;
} else {
return false;
}
}
Opinions?
Better methods??
Other C++ syntax that I'm not aware of that is better suited for this type of simple check??
Re: Best way to write a function to do something like this
Personally I'd use the switch statement... but I'd reduce it down some and use the one entry, one exit methodology:
Code:
bool isPunc(int &cc) {
bool retVal;
switch (cc) {
case cDash:
case cPeriod:
case cSpace:
retVal = true;
break;
default:
retVal = false;
}
return retVal;
}
So now if it's a Dash, Period or a Space, it'll set retVal to true... then break out of the switch.... if it's anything else, it drops into the default case and sets retVal to false. Then the last thing to do is return the result.
-tg
Re: Best way to write a function to do something like this
Thank you so much - I didn't know that syntax was allowed - it's easy on the eyes - easy to maintain - thanks!
I guess CASE without BREAK makes sense as a "drop through" - I always wondered what situations would lead to someone using a CASE without BREAKS - and this is one!