Musings from my desk

2024-12-06 11:54:24 -0600 CST

Miscellaneous coding preference: return early rather than nesting business logic inside a conditional.

Bad:

function example() {
  const optional = returnOptional()

  if (optional) {
    // all business logic,
    // including additional nestings
  }
  // implicit `return undefined`
}

Good:

function example() {
  const optional = returnOptional()

  if (!optional) {
    // explicit return value,
    // good for clarity and intentionality
    return null
  }

  // all business logic,
  // starting at the top-level of nesting!
}
Back to micro blog