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.