Line of Sight in Code

Keep the happy path left-aligned so a reader can scan one column to understand the expected flow. Handle errors and edge cases first, then let the core logic run un-indented. Based on Mat Ryer’s article.

Rules

  • Align the happy path to the left edge.
  • Exit early — handle errors first, return/abort, don’t wrap the rest in else.
  • Flip if conditions so the short exit comes first and the happy path stays un-indented.
  • Put the happy-path return as the very last line.
  • Extract methods to keep bodies small and readable.

Examples

Exit early, don’t nest the happy path:

# bad
def charge(user)
  if user
    if user.card
      if user.card.valid?
        process_payment(user)
      else
        :invalid_card
      end
    else
      :no_card
    end
  else
    :no_user
  end
end

# good
def charge(user)
  return :no_user unless user
  return :no_card unless user.card
  return :invalid_card unless user.card.valid?

  process_payment(user)
end

Flip the condition, avoid else:

# bad
def process(user)
  if user.active?
    do_work(user)
    send_notification(user)
  else
    raise "inactive user"
  end
end

# good
def process(user)
  raise "inactive user" unless user.active?

  do_work(user)
  send_notification(user)
end