Red Squirrel Reflections
Dave Hoover explores the psychology of software development


[Previous entry: "Learn about Need-Driven Development"] [Main Index] [Next entry: "Gridblogging: Fatherly Advice"]

Any fool can write code that a computer can understand
Monday, December 13, 2004

Just a little piece on writing understandable code.

Posted by Dave

Replies: 5 comments

Something I picked up from James Ross...

Don't just write code that people can read; write code that people can listen to.

Posted by Jason Yip on 12/13/2004

I don't know the details of all implementations of Map, but doesn't your code end up performing the lookup twice and the original code only once? Obviously that's an optimization question and one shouldn't optimize early, but given that this is a cache example it may be a valid reason for using the slightly less readable version.

Posted by Brian McKeough on 12/13/2004

Yes, the refactored code does the lookup twice. No doubt, this would be a valid reason to use the original version, once the need for better performance presents itself.

Posted by Dave Hoover on 12/13/2004

I think that the snippet is so small that both versions are equally comprehensible. What is important in understanding the code is sensible, consistent naming of concepts and clear expression of the relationships, especially dynamic relationships, between those concepts.

Posted by Nat Pryce on 12/14/2004

You should prefer containsKey over the null-check in any case as get(Object key) inserts a new, default value of null into a map if key is not present.

Posted by B. K. Oxley (binkley) on 12/15/2004

Powered by Greymatter