DEV Community

Cover image for "Tell, Don't Ask" Principle Explained in 100 Seconds
Dzung Nguyen
Dzung Nguyen

Posted on โ€ข Edited on

2

"Tell, Don't Ask" Principle Explained in 100 Seconds

The Tell, Don't Ask principle is a core concept in object-oriented programming (OOP) that encourages designing objects to contain both data and the behaviors that operate on that data. This principle helps create more maintainable and robust systems by promoting better encapsulation.


๐Ÿ’ก What is "Tell, Donโ€™t Ask"?

๐Ÿ’ข Tell, Donโ€™t Ask is a principle that reminds us to tell objects what to do rather than asking them for data and acting on it outside of the object. It emphasizes moving behavior into objects alongside their data, keeping logic and state together.

๐Ÿ’ข Instead of extracting data from an object and making decisions EXTERNALLY, you tell the object to perform an action INTERNALLY. This approach simplifies code, reduces coupling, and makes systems easier to extend and maintain.

Principle Image


๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆ Example: Monitoring a Value

Letโ€™s consider an example where we need to monitor a sensorโ€™s value and trigger an alarm if the value goes above a certain limit.

Asking for Data ("Ask" Style)

class AskMonitor {
  private int value;
  private int limit;
  private String name;
  private Alarm alarm;

  public AskMonitor(String name, int limit, Alarm alarm) {
    this.name = name;
    this.limit = limit;
    this.alarm = alarm;
  }

  public int getValue() { return value; }
  public void setValue(int value) { this.value = value; }
  public int getLimit() { return limit; }
  public String getName() { return name; }
  public Alarm getAlarm() { return alarm; }
}
Enter fullscreen mode Exit fullscreen mode

We use it like this:

AskMonitor monitor = new AskMonitor("Temperature Sensor", 100, alarm);
monitor.setValue(120);

if (monitor.getValue() > monitor.getLimit()) {
  monitor.getAlarm().warn(monitor.getName() + " is too high");
}
Enter fullscreen mode Exit fullscreen mode

Telling the Object ("Tell" Style)

In "Tell, Donโ€™t Ask," we move the behavior into the Monitor class.

class TellMonitor {
  private int value;
  private int limit;
  private String name;
  private Alarm alarm;

  public TellMonitor(String name, int limit, Alarm alarm) {
    this.name = name;
    this.limit = limit;
    this.alarm = alarm;
  }

  public void setValue(int value) {
    this.value = value;

    if (this.value > this.limit) {
      alarm.warn(name + " is too high");
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

We use it like this:

TellMonitor monitor = new TellMonitor("Temperature Sensor", 100, alarm);
monitor.setValue(120);
Enter fullscreen mode Exit fullscreen mode

Notice how the "Tell" version eliminates external decision-making by embedding the logic directly into the setValue method.


โญ Benefits

โœ… Encapsulation: Combines data and behavior, keeping them tightly related.
โœ… Simplified Code: Reduces external logic by placing behavior inside relevant objects.
โœ… Maintainability: Easier to update and extend functionality.


๐Ÿ’Ž Use "Tell" When:

โœ… Encapsulating Behavior: When the object knows what it needs to do with its data and should act upon it directly to make it easier to maintain and avoid inconsistent states.

Example: A Monitor object has a limit for its value and should raise an alarm when the value exceeds that limit. Rather than asking the monitor for its value and then acting on it, you tell the monitor to take action when the value is set.

โœ… State Changes Triggering Actions: When an update in one part of the object's state requires a follow-up action (like triggering a notification, logging, or updating other components), the object itself should manage those actions.

Example: A UserProfile object might automatically update a userโ€™s activity log whenever their profile information is updated.


๐Ÿ’Ž Use "Ask" When:

โœ… Queries for Data: When you need data from the object without altering its state or causing any action.

Example: Asking a User object for its email address or phone without needing any other action.

โœ… External Decisions: When decisions depend on external data or logic that the object shouldnโ€™t handle.

Example: You might ask a Person object for its name to decide externally how to greet the person (e.g., choosing between "Hello, Mr. X" or "Welcome back, Mr. X").

โœ… Delegation of Responsibility: When objects collaborate and one object needs data from another to determine the next action, avoiding too much logic in a single object.

Example: In a network of objects, a Router might "ask" a Server for its load and make decisions about traffic management based on the current state.


Ultimately, adopting Tell, Donโ€™t Ask fosters better encapsulation and cleaner code. It allows for a more natural flow of interactions between objects, When implemented effectively, this principle contributes to building scalable and maintainable software systems.

๐Ÿ“ฐ Others

Interested? ๐Ÿ˜ƒ Check out other posts from my programming principles series!


Follow me to stay updated with my future posts:

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but itโ€™s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

๐Ÿ‘‹ Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay