---
title: Tell, Don't Ask
teaser:
tags: web,ruby,good code
author: Ben Orenstein
published_on: 2012-07-19
---

And now, a friendly message from your local [Tell, don't
ask](http://pragprog.com/articles/tell-dont-ask) Department.

## Example 1

Not so good:

```ruby
<% if current_user.admin? %>
  <%= current_user.admin_welcome_message %>
<% else %>
  <%= current_user.user_welcome_message %>
<% end %>
```

Better:

```ruby
<%= current_user.welcome_message %>
```

## Example 2

Not so good:

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

Better:

```ruby
system_monitor.check_for_overheating

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

## Example 3

Not so good:

```ruby
class Post
  def send_to_feed
    if user.is_a?(TwitterUser)
      user.send_to_feed(contents)
    end
  end
end
```

Better:

```ruby
class Post
  def send_to_feed
    user.send_to_feed(contents)
  end
end

class TwitterUser
  def send_to_feed(contents)
    twitter_client.post_to_feed(contents)
  end
end

class EmailUser
  def send_to_feed(contents)
    # no-op.
  end
end
```

## Example 4

Not so good:

```ruby
def street_name(user)
  if user.address
    user.address.street_name
  else
    'No street name on file'
  end
end
```

Better:

```ruby
def street_name(user)
  user.address.street_name
end

class User
  def address
    @address || NullAddress.new
  end
end

class NullAddress
  def street_name
    'No street name on file'
  end
end
```

Good <abbr title="Object Oriented Programming">OOP</abbr> is about telling
objects what you want done, not querying an object and acting on its behalf.
Data and operations that depend on that data belong in the same object.

Tell, don't ask!

### Next Steps & Related Reading

* [A general treatment of Tell Don't Ask][tda]
* [Tell Don't Ask and Mock Objects][tda-mocks]

[tda]: http://pragprog.com/articles/tell-dont-ask
[tda-mocks]: http://www.mockobjects.com/2006/10/tell-dont-ask-and-mock-objects.html

Detect emerging problems in your codebase with [Ruby Science][ruby-science].
We'll deliver solutions for fixing them, and demonstrate techniques for
building a Ruby on Rails application that will be fun to work on for years to
come.

[Grab a free sample of Ruby Science today!][ruby-science]

[ruby-science]: http://rubyscience.com?utm_source=giantrobots&amp;utm_medium=blog&amp;utm_campaign=remarket&amp;utm_term=testing
