I am porting some code from C to C++ and I found this code:
if(ErrorCode >= SOME_CONSTANT)
{
Status = RETVAL_OK;
switch ( ErrorCode )
{
default:
Status = RETVAL_FAILED;
break;
}
}
This code generates a compilation warning:
warning C4065: switch statement contains 'default' but no 'case' labels
The question: Is there any purpose of the switch statement (that I failed to grasp) or is it simply crufty code?
That is, is there any reason (when compiling for ANSI C or C++) to not write it like this?
if(ErrorCode >= SOME_CONSTANT)
Status = RETVAL_FAILED;
EDIT: To address all the questions that appeared:
The code was not meant to be expanded: It was the final release of a module that was delivered four years ago (it hasn’t been touched since then, so I am inclined to believe it’s cruft).
There were also no removed case statements as far as I could see (the same code construct was placed in three different places in code (the same switch/default inside an if checking on the error constant). If there were removed case statements, the code should have been refactored anyway when the cases were removed.
Thanks everyone.
Two things I can think of: 1) the code was automatically generated 2) the original coder thought they might add different processing for error codes later, but never did.
In either case, I can’t see any reason not to change it to a simple if statement