Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aren't Information Expert & Tell Don't Ask at odds with Single Responsibility Principle?

Tags:

Information-Expert, Tell-Don't-Ask, and SRP are often mentioned together as best practices. But I think they are at odds. Here is what I'm talking about.

Code that favors SRP but violates Tell-Don't-Ask & Info-Expert:

Customer bob = ...; // TransferObjectFactory has to use Customer's accessors to do its work,  // violates Tell Don't Ask CustomerDTO dto = TransferObjectFactory.createFrom(bob);  

Code that favors Tell-Don't-Ask & Info-Expert but violates SRP:

Customer bob = ...; // Now Customer is doing more than just representing the domain concept of Customer, // violates SRP CustomerDTO dto = bob.toDTO(); 

Please fill me in on how these practices can co-exist peacefully.

Definitions of the terms,

  • Information Expert: objects that have the data needed for an operation should host the operation.

  • Tell Don't Ask: don't ask objects for data in order to do work; tell the objects to do the work.

  • Single Responsibility Principle: each object should have a narrowly defined responsibility.

like image 498
moffdub Avatar asked Oct 04 '08 00:10

moffdub


1 Answers

I don't think that they are so much at odds as they are emphasizing different things that will cause you pain. One is about structuring code to make it clear where particular responsibilities are and reducing coupling, the other is about reducing the reasons to modify a class.

We all have to make decisions each and every day about how to structure code and what dependencies we are willing to introduce into designs.

We have built up a lot of useful guidelines, maxims and patterns that can help us to make the decisions.

Each of these is useful to detect different kinds of problems that could be present in our designs. For any specific problem that you may be looking at there will be a sweet spot somewhere.

The different guidelines do contradict each other. Just applying every piece of guidance you have heard or read will not make your design better.

For the specific problem you are looking at today you need to decide what the most important factors that are likely to cause you pain are.

like image 57
Hamish Smith Avatar answered Sep 20 '22 06:09

Hamish Smith