A coding confession

I just found this code in one of my source files:

for (HashMap.Entry<String, Void> e2: map.entrySet()) {
  if (eventName.equals(e2.getKey())) {
    m.handle(eventName, data);

Yes. I was iterating through the keys in a HashMap to find out whether the one I wanted was there. But the whole point of a HashMap is that looking up a key is an O(1) operation.

What I should have written is:

if (map.containsKey(eventName)) {
  m.handle(eventName, data);

The moral of the story: I can be really, really dumb. (But hey, at least my code worked!)

6 responses to “A coding confession

  1. Everyone can be “really, really dumb”. That’s part of the process of becoming (hopefully) less-dumb. I dumb all the time. The aim is to try to not redo the same dumbs (: Dumb new dumbs! Learn from the dumbs! Share the learning to reduce the dumbage of others (: Learn from the dumbage of others (:

  2. I do this all the time. I have to remember to look this stuff up before designing complicated loops.

  3. I’ve done plenty of things like that.

    The mind likes to fall into familiar habits*, and one doesn’t often double check code which is working correctly.

    * Have you heard this old joke: Q: If a mathematician is in a hotel room and they notice a small fire and have a bucket of water handy what do they do? A: Use the bucket of water to put out the fire.

    Q: If a mathematician is in a hotel room with a bucket of water and no fire what do they do? A: Set a fire; thereby reducing the situation to the previous problem which has already been solved.

  4. Davyd, I posted this precisely in the hope that others can learn from my dumbage.

    John, welcome to the blog :-)

    femtohacker, I’ve not heard that precise joke, but others in a similar vein. They always make me smile.

  5. Maarten Daalder

    A Map? Surely that is a Set? (Or are you using an IdentityHashMap as a Set? I’ve done that a couple of times… :P)

    Anyway, I once had a small bug, did a similar loop and when the value was null I returned early, except instead of returning I ought to have been calculating a value that was dependant on some other value(s)… Oops…

    I startled awake that night bathing in sweat unable to sleep again until I had gone through my mind with what could have frightened me that much and fixed that mistake. :)

  6. Yes, a Set would have been a better representation for what I was doing here. This comes from trying to move to Java from Perl and Ruby, where hashes are so ingrained as parts of the language that you just use them all the time without thinking about it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.