Thursday, September 23, 2010

Case insensitive Map key - code smell

Here's the bug that had me working today (a Sukot holiday):

_myMap.get(key) // without lower casing the key.

At first you might think of this as a common human error, but I claim that it's no less of a code smell:

Why trust yourself to always remember to lower/upper case all of the interactions with the map? What about trusting others?
So, instead of using a HashMap, use an Apache CaseInsensitiveMap that nicely and safely encapsulates this key's case concern.

I would expect CaseInsensitiveMap to become a part of the Java SDK.

1 comment:

  1. I follow your blog for a while and use your hints from time to time. Thanks for job you do! At the moment I'm working over data rooms and your info is very helpful.