Tags: #code #coding #programming #earlyreturns

Prefer early returns in the functions. It makes your code flatter and easier to read.

auto MyClass::updateDifficulty(int val) -> bool
{
  const auto cfg = getCfg();
  if (!cfg)
    return false; // <--- early return

  if (val < cfg->minDifficulty() || val >= cfg->maxDifficulty())
    return false; // <--- early return

  for (auto &u: units)
    u.setDifficulty(val);

  return true;
}

Next - Previous