Untangling the God Object
werve.net
werve.net
Lo and behold, you created a ProductSearcher class. It seems to me that the initial approach stems not from the view that "objects are things" but rather the notion that any related property of an object should be included in the class definition of the object, which can be summed up as a misunderstanding of object-oriented programming more so than anything else, but maybe I'm looking at this the wrong way.
Interesting article, thanks for sharing.
Given how much I've seen this sort of stuff in the wild, I felt it would be a useful article to hand off to new-ish developers (I'm the author).
It sounds like you're well beyond that, though, but thanks for reading nonetheless!
Never, ever, ever be afraid to add a class. If you've chosen an OOP language, then you have to make classes, and make them willy-nilly. Always better to create a class and then consolidate its commonalities with another class into a common base than to try and shoehorn too much logic into a single class where all its uses are intertwingled together.
The idea of just creating other objects out of rainbow tears and unicorn dust doesn't naturally flow from this style of learning to program. :)
The classic example (both for Ruby and Scala, I believe) is to use mixins to add iteration behavior to a class. This is a perfect example -- to my mind -- of where mixins really shine.
Where things get dangerous is when people start breaking objects apart into mixins because the class has gotten too big (rather than using delegation and separating concerns).
Everything is still tightly coupled together -- none of the mixins can work independently of each other, or be used by other classes. It's very difficult to test, and when a project goes crazy with this pattern, you spend a lot of time playing Hunt The Wumpus, trying to figure out exactly where behavior is defined.
I do not like Hunt the Wumpus. :)
Do keep in mind -- I've only done the tiniest amount of Scala programming, so take what I have to say with the proverbial salt-grain. But from what I understand, Scala mixins work much like Ruby mixins -- not exactly the same, but they fill a similar role.
Also, see virtual classes, though very few languages have first-class support for those these days.
Scala mixins (traits) are basically classes that undergo liearized multiple inheritance; they are designed to work well in a statically typed language (they can be used as constraints for type members, and they can refer to those type members themselves, leading to some nice constructions).
Code Climate is a pretty helpful service by the by.
[1] http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decomp...
How does it go together?
The point isn't to create "NounVerber" classes as a solution for everything, but to break your objects apart according to the roles that they play in solving the problem.
Thinking of objects as actors has helped me with this over the years.
In the example "ProductSearcher" should probably have been named "ProductSearch" -- you can instantiate it, pass it around, and so on, so it doesn't quite behave like many the "NounVerber" objects in Javaland (e.g., AbstractFactoryBeanBuilderImpl).
When you couple this with a functional approach -- where you separate transformation and state -- magic happens. But that's a future article. :)
Ya, how often do you see that in OO programming?
I think you would only find such monstrosities in Ecliopse.
Apache XML-RPC[0]:
RequestProcessorFactoryFactory.RequestProcessorFactory.getRequestProcessor(XmlRpcRequest)
Spring[1]: (no double-factory, but it's got all of the fun words) AbstractSingletonProxyFactoryBean
[0]: https://ws.apache.org/xmlrpc/apidocs/org/apache/xmlrpc/serve...[1]: http://docs.spring.io/spring/docs/2.5.x/api/org/springframew...
InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
http://javapapers.com/core-java/longest-class-method-and-att...