Have you ever used a library in a project, only to find that it depends on other libraries, only to depend on other libraries?
I’m willing to bet that you have. Fortunately, there exist tools in the world to help manage these dependencies. In the Java world, Maven is the de facto standard in this regard. It’s quite convenient to manage your dependencies. On Windows, NuGet is the de facto standard. (Note: I have never used NuGet. The number of dependencies can quickly get staggeringly huge though, so that is at least part of the reason that these tools exist.
Why do I bring this up? Well, at work we use jOOQ for our database operations. It’s an awesome library that you should use if you need to do database operations in Java. They also have a blog that I read from time to time, as there is some good information included in it. One of their more recent blog posts talks about libraries and having zero dependencies. Basically, the argument here is that all libraries should have zero dependencies, because of getting into a dependency hell(although they do carve out an exception for frameworks such as Spring and Java EE).
I don’t fully agree with this statement, but it is a good time to talk about these dependencies. As far as I am able, none of the libraries that I have made have any dependencies(that being said, I only have one library on Maven: JavaSerial).
Why don’t I fully agree with the statement? Well, in many cases I don’t think that it is entirely unreasonable to have a dependency, although this can get into a very gray area. In the case of jOOQ, it would not be entirely unreasonable to have a dependency on commons-lang, because there are two different artifacts for commons-lang(one for version 2.x[commons-lang] and one for version 3.x[commons-lang3]). On the other hand though, if there is only one class that you need from a certain dependency, it is reasonable to simply wrap that class as jOOQ does in order to provide the same level of support.
With that being said, here is a set of questions you should ask yourself when deciding if you need a hard dependency of a library:
- What is the dependency attempting to provide? For example, SLF4J is a reasonable dependency to have, since many libraries already use it for logging(although, if at all possible, you should use java.util.logging instead)
- How much of the dependency do you need? If you only need a single class from commons-lang as jOOQ does, pulling in the full dependency does not make sense. If you use a significant portion, then it may make sense. This is probably the most important question you have to ask. If you are doing a lot of reflection, then it may be worth it to get a library to help you do reflection. If you only need to do it once, it probably doesn’t make sense.
- How widespread is the library? Commons-lang is a widely used library, so having it as a dependency is not likely to cause problems.
- Are there different versions of the library that people may be using? As shown in the jOOQ blog post, if you are using Guava 15 while another library uses Guava 18, will that cause problems? I have never used Guava, so I don’t know if it would actually cause a problem, depending on how the API has changed.
I realize that this is not a comprehensive list, nor is it a perfect guide. Ultimately, as a library writer you should try as much as possible to keep the number of dependencies down as low as possible. Otherwise, it can quickly lead to dependency hell for the end users, and potentially for you as well.
Leave a Reply