Tell, Don’t Ask
Last updated
What is Tell, Don’t Ask?
The Tell, Don’t Ask principle emphasizes that objects should be told what to do rather than being queried for their state and having decisions made externally. This promotes encapsulation and reduces coupling by keeping logic within the objects that own the data.
Key Concepts
- Instead of pulling data out of objects to make decisions, push the behavior into the object itself.
- Objects should be responsible for their own logic and state management.
Asking style (bad):
if (user.profile.isComplete()) {
// allow checkout
}
Telling style (good):
if (user.canCheckout()) {
// allow checkout
}
Free resources to learn Tell, Don’t Ask
- ArticleTell, Don't Ask
More in stage 4: Design Principles
Tell, Don’t Ask: common questions
Can I learn Tell, Don’t Ask in Hindi?
This lesson is written in English. Once you sign in, open it in the interactive course and ask the AI tutor to explain Tell, Don’t Ask in Hindi or Hinglish. The tutor answers from this lesson’s own text, so it stays on the syllabus, and it runs on your own Google, OpenAI or Anthropic key.
Is this Tell, Don’t Ask tutorial for beginners?
It is lesson 35 of 94 in the free Software Design and Architecture roadmap, in stage 4, Design Principles. If you are new to Software Design and Architecture, start from stage 1 and come back here in order; if you already know the earlier stages, this lesson stands on its own.