...
Code Block |
---|
|
int string_loop(char *str) {
size_t i;
size_t len = strlen(str);
for (i=0; i < len; i++) {
/* ... */
if (str[i+1] == '\0')
/* This code is now reached */
}
return 0;
}
|
Exceptions
MSC07-EX1: In some situations,
seemingly dead code may make software resilient
to future changes. An example
of this is
adding a default case to the default
label in a
switch
statement
even when all possible switch labels are specifiedwhose controlling expression has an enumerated type and that specifies labels for all enumerations of the type. (See recommendation
MSC01-C. Strive for logical completeness.)
Since valid values of an enumerated type include all those of its underlying integer type, unless enumeration constants have been provided for all those values the default
label is appropriate and necessary. Code Block |
---|
|
typedef enum { Red, Green, Blue } Color;
const char* f(Color c) {
case (c) {
switch Red: return "Red";
switch Green: return "Green";
switch Blue: return "Blue";
default: return "Unknown color"; /* not dead code */
}
}
void g() {
Color unknown = (Color)123;
puts(f(unknown));
}
|
MSC07-EX2: It is also permissible to temporarily remove code that may be needed later. (See recommendation
MSC04-C. Use comments consistently and in a readable fashion for an illustration.)
...