Monday, June 20, 2011

Using libraries

I occasionally get flak for recommending Guava as a solution on relatively simple questions on StackOverflow. The argument generally goes: "this is so easy, don't add a library just for that!". The key phrase there is "just for that": it seems that some people on StackOverflow tend to take the viewpoint that whatever is being asked in the question is all the person who asked will need to do and that as such the only valid answer is some standalone code they can copy and paste in. I don't believe that a whole library is worth it to save yourself from writing one 4-line method... I just believe code doesn't exist in isolation and that in almost any project that's more than a few classes, Guava will be more than worth its weight.

The point of Guava is that it makes a lot of things easier, from fairly easy things that take several lines of code (but should just take one) to very hard things that would be easy to get wrong. In aggregate, using its facilities instead of writing extra code yourself can make your application clearer and more maintainable. There's also the benefit that people who have used the library before will know where to look for things instead of having to figure out what class in your project (if any) you copy/pasted some certain utility method into.

I also believe that it's up to the person who asked the question to evaluate whether they want to add a library or not. Someone almost always posts an alternative answer that does not use a library, so that option is generally going to be on the table. My hope is that they'll say "oh, nice that someone has made something that fixes this little pain point for me", take a look at what else it offers and recognize the value it'll provide them. I hope this because I genuinely believe they'll be better off if they do.

I'm aware that there are various concerns such as company policy that can get in the way of using a 3rd party library and that opinions differ on what libraries are good to use, but in general I think answers that make use of libraries are perfectly valid even when they aren't necessarily a big savings over writing the code yourself for the problem at hand.


eneveu said...

I agree 100%.

Joshua Bloch even has a specific item related to this in Effective Java (second edition): "Item 47: Know and use the libraries".

His main point is that, "by using a standard library, you take advantage of the knowledge of the experts who wrote it and the experience of those who used it before you".

Other advantages he mentions:
- you don't waste your time reinventing the wheel, focusing instead on your core business needs
- the performance of library code improves over time, and new functionalities are added

On SO, I'm glad when someone suggests a library that does the job, instead of reinventing the wheel. If it makes sense to add the library, I'll add it. If not, I can at least look at the code to get inspiration, and include in my code the bits I'm interested in (with a comment pointing to the library in case I later realize I need to do more).

All this being said, Guava is a no-brainer.

Post a Comment