Skip to content

Instantly share code, notes, and snippets.

@McZonk
Created April 17, 2013 10:55
Show Gist options
  • Save McZonk/5403398 to your computer and use it in GitHub Desktop.
Save McZonk/5403398 to your computer and use it in GitHub Desktop.
If you use switch(enum) you should not implement default: You will get a helpful warning when a value is not implemented. This is useful when new values will be added to the enum. You don't forget to handle the new value because of the warning. When a value doesn't need to be handle, add it at the end of the switch with a break.
typedef enum MyEnum {
MyEnum1,
MyEnum2,
MyEnum3,
} MyEnum;
static int function1(MyEnum e) {
switch(e) { // warning: Enumeration value 'MyEnum3' not handled in switch
case MyEnum1:
return 1;
case MyEnum2:
return 2;
}
return 0;
}
static int function2(MyEnum e) {
switch(e) {
case MyEnum1:
return 1;
case MyEnum2:
return 2;
case MyEnum3: // implement unused enum values this way.
break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment