Strongly Typed Collections


Martin Fowler writes about strongly typed collections, and concludes:

“On the whole, however, it isn’t worth the trouble”  

I must admit that I disagree.  In anything but the smallest of projects it is both unconvenient and unhealthy to use untyped collections. Typed collections provide type-safety,better encapsulation and cleaner code. I would much rather invest 20 lines of code to inherit from CollectionBase than have explicit casts scattered all over my app (which are hard to debug and even harder to maintain).  And it is a good practice going forward because when generics become available (Whidbey) we will get all the advantages of typed collections for ~1 line of code.

In Martins defense, his code samples seem to be mostly Java so he probably means “On the whole, however, it isn’t worth the trouble IN JAVA and as we all know, nothing is worth the trouble of doing in Java 😉

 

Comments (4)

  1. Anonymous says:

    I disagree with your argument. First of all cut the petulant Java vs. C# stuff. C# and Java work the same way with collections now and by the time the next version of C# ships with generics the next version of Java will also be shipping with generics if Sun and Microsoft’s timelines are to be believed.

    Secondly, I’ve never heard of anyone whose code blew up because an Employee class accidentally ended up in a hashtable or list that was only supposed to contain Automobile classes (for example).

  2. Anonymous says:

    I agree that type safety isn’t the main issue. For me the issues are convenience (=intellisense in the IDE) and maintainability. Anything which is not explicitly designed to appear only once in the app won’t appear twice – it will appear hundreds of times. And when the time comes (usually at 2am just before the release), do you really want to start replacing all those hard coded typecasting statements? Is it really so much trouble to add in those 20-30 lines of code to create a typed collection?

    And why should I have to put up with ugly code such as this: (in VB.NET just to maximize the pain)

    CType(CType(MyParentObjects(i),ParentObject).ChildObjects(j),ChildObject).myProperty

    instead of plain & simple:

    MyParentObjects(i).ChildObjects(j).myProperty

    Now, what doesn’t make sense about that?

  3. Anonymous says:

    I vote for "definitley worth the time", and along those lines a Typed Dataset is very useful, too.

  4. Anonymous says:

    I’ve never used strongly typed collections before but for a little side learning project I decided to. It was really simple and the benefit of type safety seemed to be worth it. But it’s not really that big of a gain in the whole scheme of things. So while I thought I was missing out on something big, it’s really not that big of a deal and should be even more of a mute point once we have generics. Anywhere, here are few references just in case I decide to revisit this in the future. http://www.samspublishing.com/articles/article.asp?p=27578 http://builder.com.com/5100-6373-1050004.html http://www.ondotnet.com/pub/a/dotnet/2003/03/10/collections.html And a little debate on the topic http://weblogs.asp.net/asanto/archive/2003/06/24/9201.aspx