Nithin Bekal About

Notes / Tell Don't Ask

  • Tell your code to do something, don’t ask it for internal state
  • Avoid low level details

Procedural code gets information then makes decisions. Object-oriented code tells objects to do things.

– Alec Sharpe, Smalltalk by Example

endeavor to tell objects what you want them to do; do not ask them questions about their state, make a decision, and then tell them what to do.

pragprog blog

Making decisions outside the object violates encapsulation

An example from the Thoughtbot blog:

def check_for_overheating(system_monitor)
  if system_monitor.temperature > 100
    system_monitor.sound_alarms
  end
end

Better:

system_monitor.check_for_overheating

class SystemMonitor
  def check_for_overheating
    if temperature > 100
      sound_alarms
    end
  end
end

Checking is_a? in Ruby is another type of tell-dont-ask violation.