This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Golden Core Hammer
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
I've been reading lots of articles on what the .NET community calls "strongly typed collections". These are collections that implement their own methods for Add, Remove, Contains, and so forth but take a parameter of a specific class instead of just Object. The argument is that these strongly typed collections don't have "dangerous downcasts" when removing elements from the collection and prevent you from writing inappropriate objects into the collection. A particularly good article describing this is from OnDotNet.
An example of such a collection is provided on the Human Compiler blog. At the end, the author states:
This may look like a lot of code, but it's really not. It took me all of 15 minutes to setup from scratch. Now you can use it just like any other Strongly Typed Collection.
My question is - what's the big deal? Collections are almost always encapsulated inside other more interesting objects. The Law of Demeter states that you shouldn't extract a collection from inside an object and proceed to send messages to it. It breaks encapsulation. Instead, you should use Encapsulated Collections. There's no sense having all this code for strongly typed collections when the collection is encapsulated already. Encapsulated Collections involves similar code to strongly typed collections but doesn't encourage people to break encapsulation.
This seems to be a very prevalent pattern in the .NET world. I don't know if I'm the only one (besides Martin Fowler), but I see it as a golden core hammer: You can't see, but it's beautiful on the inside. For anyone to see the gold, you have to break the hammer.