Should I make my code dependent on external libraries? : <p>Tl;dr: Stick to the bold text.</p>

<p><strong>Libraries</strong> provided by others can <strong>save</strong> a lot of <strong>programming time</strong>, because one does not have to solve problems that others already did. Furthermore, they often perform certain tasks much <strong>more efficient</strong> than one could ever achieve by oneself.</p>

<p>On the downside one adds a <strong>dependency</strong> to the program which can cause <strong>problems with licensing, compiling</strong> on other machines and so on. Also developments in the libraries may interfere with developments in ones own code. In extreme cases one is after some time <strong>restricted</strong> to the <strong>functionalities</strong> the library provides meaning that even a small extension might require the programmer to rewrite half of the library. In such a case one may rather want to <strong>exchange the library</strong> with a different one.</p>

<p>At this point one can be in big <strong>trouble</strong> if the whole code is cluttered with calls to the library. To prevent problems like this one can right from the start write a <strong>wrapper</strong> around the external library so that a library change reduces to changing the wrapper and no other code needs to be touched - in theory.</p>

<p>In practice, however, the <strong>interfaces</strong> through which the wrapper is called may <strong>not</strong> be <strong>compatible</strong> with the "new" library. Also a library might use <strong>data structures</strong> that are <strong>not</strong> directly <strong>compatible</strong> with the data types in ones own program. Then data needs to be reorganized and probably a lot of copying happens which was not necessary before.</p>

<p><strong>Questions:</strong></p>

<ul>
<li><strong>How can I avoid trouble with changing libraries?</strong></li>
<li><strong>Should I always wrap the functions external libraries provide?</strong></li>
<li><strong>Should I wrap data objects of external libraries as well?</strong></li>
<li><strong>Or should I instead completely decide for a library and stick with it?</strong></li>
</ul>

<p><strong>Example:</strong></p>

<p>I work on a huge program in which problems of linear algebra are ubiquitous. Recently, we started to switch to Eigen, an efficient library with broad linear algebra functionalities. Eigen comes with its own data objects. Now there are tons of <code>std::vector&lt;double&gt;</code> objects present in the code which would need to be replaced with Eigen's <code>VectorXd</code> to be able to nicely work with Eigen. It would be a hell of a work to do all these replacements. However, it would probably be even more work to undo these changes if Eigen at some points turns out to be not the ideal solution. Now I'm not sure whether writing an own vector class which just wraps the Eigen data type would actually reduce the effort if the library will be exchanged someday, or whether I will just produce more problems that way.</p>
