How do you decide whether to use a library or write your own implementation

General Decision

Before deciding on what to use, I will create a list of criteria that must be met by the library. This could include size, simplicity, integration points, speed, problem complexity, dependencies, external constraints, and license. Depending on the situation the factors involved in making the decision will differ.

Generally, I will hunt for a suitable library that solves the problem before writing my own implementation. If I have to write my own, I will read up on appropriate algorithms and seek ideas from other implementations (e.g., in a different language).

If, after all the aspects described below, I can find no suitable library or source code, and I have searched (and asked on suitable forums), then I will develop my own implementation.

Complexity

If the task is relatively simple (e.g., a MultiValueMap class), then:

  1. Find an existing open-source implementation.
  2. Integrate the code.
  3. Rewrite it, or trim it down, if it excessive.

If the task is complex (e.g., a flexible object-oriented graphing library), then:

  1. Find an open-source implementation that compiles (out-of-the-box).
  2. Execute its “Hello, world!” equivalent.
  3. Perform any other evaluations as required.
  4. Determine its suitability based on the problem domain criteria.

Speed

If the library is too slow, then:

  1. Profile it.
  2. Optimize it.
  3. Contribute the results back to the community.

If the code is too complex to be optimized, and speed is a factor, discuss it with the community and provide profiling details. Otherwise, look for an equivalent, but faster (possibly less feature-rich) library.

API

If the API is not simple, then:

  • Write a facade and contribute it back to the community.
  • Or find a simpler API.

Size

If the compiled library is too large, then:

  • Compile only the necessary source files.
  • Or find a smaller library.

Bugs

If the library does not compile out of the box, seek alternatives.

Dependencies

If the library depends on scores of external libraries, seek alternatives.

Documentation

If there is insufficient documentation (e.g., user manuals, installation guides, examples, source code comments), seek alternatives.

Time Constraints

If there is ample time to find an optimal solution, then do so. Often there is not sufficient time to write from scratch. And usually there are a number of similar libraries to evaluate. Keep in mind that, by meticulous loose coupling, you can always swap one library for another. Find what works, initially, and if it later becomes a burden, replace it.

Development Environment

If the library is tied to a specific development environment, seek alternatives.

License

Open source.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)