Should you wrap 3rd party libraries that you adopt into your project?

flybywire picture flybywire · Dec 16, 2009 · Viewed 7.4k times · Source

A discussion I had with a colleague today.

He claims whenever you use a 3rd party library, you should always write for it a wrapper. So you can always change things later and accomodate things for your specific use.

I disagree with the word always, the discussion arose regarding log4j and I claimed that log4j has well tested and time proven API and implementation, and everything thinkable can be configured a posteriori and there is nothing you should wrap. Even if you wanted to wrap there are proven wrappers like commons-logging and log5j.

Another example that we touched in our discussion is Hibernate. I claimed that it has a very big API to be wrapped. Furthermore it has a layered API which lets you tweak its inside if you so need. My friend claimed that he still believes it should be wrapped but he didn't do it because of the size of the API (this co-worker is much veteran than me in our current project).

I claimed this, and that wrapping should be done in specific cases:

  • you are not sure how the library will fit your needs
  • you will only use a small portion of a libary (in which case you may only expose a part of its API).
  • you are not sure of the quality of the library's API or implementation.

I also maintained that sometimes you can wrap your code instead of the library. For example, puting your database related code in a DAO layer, instead of preemptively wrapping all of hibernate.

Well, in the end this is not really a question, but your insights, experiences and opinions are highly appreciated.

Answer

Michael Borgwardt picture Michael Borgwardt · Dec 16, 2009

It's a perfect example for YAGNI:

  • it is more work
  • it inflates your project
  • it may complicate your design
  • it has no immediate benefit
  • the scenarion you write it for may never manifest
  • when it does, your wrapper most likely needs to be re-written completely because it is tied too closely to the concrete library you were using and the new one's API simply doesn't match yours.