Created
September 14, 2020 01:34
-
-
Save kfsone/24ad2dd9334ad38fd7d0d261dcec2eeb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is a relatively common pattern: | |
| while (true) | |
| { | |
| if (predicate()) | |
| continue; | |
| ... | |
| break; | |
| } | |
| // so is this | |
| for (auto it = container.begin(); it != container.end(); it = std::next(it)) { | |
| if (it->processed()) | |
| continue; | |
| if ... | |
| // stop when we find the first bad case. | |
| break; | |
| } | |
| // so having the 'break' inside the {}s looks loopish. | |
| { | |
| if (some_condition) | |
| return false; | |
| // avoid re-execution if we can. | |
| auto* operator = predicate; | |
| for (int i = 0; i < mru.size(); ++i) { | |
| if (mru[i].predicate == predicate) { | |
| operator = &mru[i].predicate; | |
| break; | |
| } | |
| } | |
| // won't do work if the epression was already evaluated. | |
| result = operator->evaluate(); | |
| break; | |
| } | |
| //... | |
| // Having it *outside* the {}s is far more distinctive: | |
| { | |
| if (some_condition) | |
| return false; | |
| // avoid re-execution if we can. | |
| auto* operator = predicate; | |
| for (int i = 0; i < mru.size(); ++i) { | |
| if (mru[i].predicate == predicate) { | |
| operator = &mru[i].predicate; | |
| break; | |
| } | |
| } | |
| // won't do work if the epression was already evaluated. | |
| result = operator->evaluate(); | |
| } | |
| break; | |
| // It's good practice to include your break even when there's no other case: | |
| switch (condition) { | |
| case 1: ... | |
| break; | |
| case 2: ... | |
| break; | |
| case 3: | |
| { | |
| ... | |
| } | |
| break; | |
| } | |
| // And the 'break' being outside the braces has most cognitive load reduction when you're at the end of a fairly long switch: | |
| break; | |
| } | |
| default: | |
| // vs | |
| } | |
| break; | |
| default: | |
| } | |
| or | |
| break; | |
| } | |
| } | |
| vs | |
| } | |
| break; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment