I like my variant.
I dislike your variant. Gotos are appropriate when using them allows you to write clearer code than you can write without. I don't think that criterion has been satisfied in this case.
Someone told me that it is better to define method for "NotPresent" case and call it instead of goto case "Processed". Is this Gotophobia or reasonable?
Do you mean you were told it would be better to define a method for the body of the "Processed"
case, to be invoked unconditionally in that case and conditionally in the "Not Present"
case, thereby avoiding one goto
? I don't think that would help matters much. As long as the "Processed"
case body contains just a few statements, I find it clearer to keep those in the body of the switch
.
Myself, I would probably rewrite your switch
like so:
switch (curState.ToString())
{
case "NotPresent":
if (someValue != null)
{
goto case "Processed";
}
break;
case "Processed":
... *(couple code lines)*
break;
}
There's no point in branching to a case that does nothing -- as opposed to simply doing nothing in the first place -- so I removed that goto
altogether. I moved the "NotPresent" case above "Present" because that makes the pair reminiscent of a single switch section with two case labels.
I also removed the empty case for "Undefined"
, but if it were important to emphasize that no other values of curState.ToString()
were expected, then I would instead retain that case and add a default
case with appropriate assertion / throw / logging.