Skip to content

Instantly share code, notes, and snippets.

@kfsone
Created September 14, 2020 01:34
Show Gist options
  • Select an option

  • Save kfsone/24ad2dd9334ad38fd7d0d261dcec2eeb to your computer and use it in GitHub Desktop.

Select an option

Save kfsone/24ad2dd9334ad38fd7d0d261dcec2eeb to your computer and use it in GitHub Desktop.
// 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