Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joyhuang9473/d01d7ef44f6817d866e4 to your computer and use it in GitHub Desktop.
Save joyhuang9473/d01d7ef44f6817d866e4 to your computer and use it in GitHub Desktop.
If not conditional on an enumerated value, switch statements should always have a default case (in the case of an enumerated value, the compiler will warn you if any values are not handled). If the default case should never execute, simply assert:
switch (var) {
case 0: // 4 space indent
... // 8 space indent
break;
case 1:
...
break;
default:
assert(false); // If the default case should never execute, simply assert
}
// Braces are required for single-statement loops.
for (int i = 0; i < kSomeNumber; ++i) {
printf("I take it back\n");
}
while (condition) {
// Repeat test until it returns false.
}
// Empty loop bodies should use {} or continue, but not a single semicolon.
for (int i = 0; i < kSomeNumber; ++i) {} // Good - empty body.
while (condition) continue; // Good - continue indicates no logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment