|
|
[OOP Principles in Action: Tell, Don't Ask]Greg Willits, February 11, 2007 Tell, Don't Ask is a paradigm or style of object oriented programming which prefers that objectA tells objectB to do something, rather than asking objectB about its state so that objectA can make a decision. Learning the best practices of OOP often requires that we unlearn many things we were originally taught using procedural programming. Tell, Don't Ask ("TDA") is one of those areas. In the procedural programming paradigm, code is written to do some stuff, and it fetches or looks at the state of some data (the actual values of a specific variable or place of memory), and then makes a decision or takes an action. You could say that procedural programming pulls data into the logic stream in order to make decisions and get things done. In object oriented programming, we want to do the opposite. It turns out that better designs come from having objects push data out, and having other objects get things done. Of course the buck can only be passed so much, as somebody has to actually do something, and we'll look at where those lines are. It can be a bit of a mind bender to invert your way of thinking, and it can be difficult to recognize when your code is not living up to TDA if you've spent a lot of time doing procedural programming. The article uses real-world code examples to show how following TDA improves code encapsulation to help it realize the goal of class orthogonality. Files |
Articles |