For years I have been against so-called "smart" objects. (In fact, I thought I blogged on it at once time but can't seem to find it...guess I'm getting old.) I have taught and mentored that objects shouldn't know how to perform certain operations. Recently, however, I've started having a change of heart. That is NOT to say that I am changing my position. It is to say, however, that I am starting to see the other side of the coin. However, I need your feedback.
Object-Oriented Programming - In Theory
According to object-oriented lore, object-orientation is intended to allow programmers to model the "real world" (whatever that is) in their software systems. There is an old joke to this effect:
How many C++ programmers does it take to screw in a light bulb? If you answered with a number greater than zero, you're still thinking functionallity (imperatively). C++ programmers have the socket object call the "replace" method on itself.
Of course this is ridiculous...light bulb sockets don't change the bulbs themselves. But wait, an object is, by definition, "an entity which has state and behaviors that act on that state." And that's where the fun begins.
My Perspective - Smart Objects BAD!
A hundred years or so ago, Visual Basic 6 utilized the Collection class. Many VB6 programmers utilize(d) the "smart" object approach. For example, the collections would sort themselves. So here has been my argument for years.
Assumptions:
- You have a collection of magazines. You can even imagine a stack of magazines sitting on a table.
- Object-Oriented Programming models the real world.
Coding:
You write a collection class for magazines (MagazineCollection). The class has a "Sort" method.
Problem:
In the real world, magazine collections do NOT sort themselves. Go ahead and try it. Stack some magazines on your desk and ask the stack of magazines to sort itself. Let me know when it happens. (P.S. If it happens, I'll print this post and eat the paper.)
Solution:
So, in the "real" world, what sorts magazines? You do. And what are you when you are doing this? A magazine sorter. So I would implement a class called "MagazineSorter".
The same is true (in my not so humble opinion) for database operations as well. Why should objects know about how they are persisted? Of course with configuration files and such it is much easier to change the persistence mechanism and location. However, it seems to me that having an object that understands the database schema violates about a bazillion abstraction best practices.
My Perspective Change
So my good friend Brian Johnston is a HUGE CSLA fan. (I think he has "CSLA ROX" on his boxers.) Rocky and I have (obviously) disagreed for years on the whole "smart" object thing. Rocky has promoted smart objects since at least VB5 and the CSLA framework is no different.
Well, I'm trying to be open-minded about it so I gave Brian (and his cohort, Stephen Richards) props and listened as they extolled the benefits of smart-er objects and panned the downsides of "manager" classes. (In one slide, Brian even writes "Kill Your Managers" but secretly I think this has a more Freudian meaning for him.)
So I was trying to think about groups of things in the "real" world that sort themselves. True that magazines still don't (are you still trying?). However, collections of humans do. In fact, humans do a lot on their own like respirate, defecate, procreate, and eat (tip-o--da-hat to Fox Mulder).
Disclaimer: I am NOT under any circumstances trying to imply that humans are smart.
Help Please
So now I'm having a change of heart. It seems that some things in nature are smart about themselves while others are not. So here are my questions:
- Where do you draw the line between smart objects and not-so-smart objects? Are all of your objects "smart"?
- How smart are your objects? Do they know how to persist themselves in the database?
- How do you accomplish abstraction with smart objects?
Thanks in advance for any advice you have on this. I may not agree with you but I appreciate your perspective. Help an old dogg learn an old trick.
P.S. Rocky, if you're reading, I gots nuttin' but luv fo' ya. CSLA framework is amazing.