Blog Home

Understanding Code in Review

2018-05-01

If you find yourself reviewing code that is difficult to understand, don't waste time reviewing it.

Bad

bool IsOkay(int n) {
  bool f = false;
  for (int i = 2; i <= n; ++i) {
    if (n % i == 0) f = true;
  }
  return !f;
}

Ask for it to be clarified.

Good

bool IsPrime(int n) {
  for (int divisor = 2; divisor <= n / 2; ++divisor) {
    if (n % divisor == 0) return false;
  }
  return true;
}

Clarifying code often results in noticing improvements.