jeudi 14 avril 2016

Lesson of humility

I had a bad habit in Java. I've been used to check for logger level availability before using it. Stuff like:

if (logger.isDebugEnabled()) {

I've been told to do so because blah blah blah.

Seeing this made my teammates a bit mad! Therefore I took time to search for a valid explanation. And I had to admit it was useless, at least with Java 1.5+.

The usage came because for logged strings that often happen to be made of lot of information, concatenated into a single String. As you may know, concatenating Strings with + is theoretically costly since a new String instance is created every time. In the real world, the things are optimized for you to use a StringBuilder.

Prior to Java 1.5, StringBuilder did not exists and you had to use StringBuffer which is synchronized. As you can imagine, logging could then cause an overhead in a multi threaded environment like a web server.

It always hurts to be wrong. It's worse to stick with unjustified habits though. I learned a great lesson this day.