I have a state machine with some kind of callback mechanism were functions are invoked by a task runner. Behind all of that is a state machine which has 4 kinds of states, some excluding each other, others can be combined, which makes quite a complex rule set. One of the functions should show the user error messages if any illegal action is attempted (for sake of simplicity printf
here):
static int state1 = 0;
static bool switch2 = 1;
void do_stuff(int value){
int errorCode = 0;
if(state1 == 1){
errorCode = -1;
goto ERROR;
}
if(state1 == 2 && switch2)
{
errorCode = 2;
goto ERROR;
}
printf("No error!");
return;
ERROR:
printf("%d", errorCode);
}
This was the shortest and most concise way I can think of, but its always been hammered home that using goto
is a bad thing. Is there any better way to solve this problem or is this the best way in regards of stability and maintenance?