Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):
bool executeSomeAction(someValue) {
switch (someValue) {
case shouldDoAction1:
action1();
break;
case shouldDoAction2:
action2();
break;
default:
return false;
}
return true;
}
Is it bad practise to write it like this:
bool executeSomeAction(someValue) {
switch (someValue) {
case shouldDoAction1:
action1();
return true;
case shouldDoAction2:
action2();
return true;
default:
return false;
}
}
Edit: I don't see how this is a duplicate of the linked question, I'm asking about the best practise of writing a function with a single switch statement, I'm not at all asking about using a single or multiple return statements.
goto
, inheritance, mutability,switch
etc etc etc. The problem is, in 99.999% of the time, it isn't used properly. So "don't use feature x" is actually excellent advice. It can then be safely ignored by the tiny group of folk who understand about edge cases where it is needed and only use it in those cases. And from experience those who says "x is a perfectly good feature when used properly" don't know when to use it properly.