tags:

views:

225

answers:

5

Inspired by this question which started out innocently but is turning into a major flame war.

Let's say you need to a utility method - reasonably straightforward but not a one-liner. Quoted question was how to repeat a string X times. How do you decide whether to use a 3rd party implementation or write your own?

The obvious downside to 3rd party approach is you're adding a dependency to your code. But if you're writing your own you need to code it, test it, (maybe) profile it so you'll likely end up spending more time.

I know the decision itself is subjective, but criteria you use to arrive at it should not be.

So, what criteria do you use to decide when to write your own code?

+3  A: 

If it's a trivial function, it's not worth pulling in an entire library.

If it's a non-trivial function, then it may be worth it.

If it's multiple functions which can all be handled by pulling in a single library, it's almost definitely worth it.

Amber
+1  A: 

10 questions ...

+++ (use library) ... --- (write own library)

  1. Is the library exactly what I need? Customizable in a few steps? +++
  2. Does it provide almost all functionality? Easily extensible? +++
  3. No time? +++
  4. It's good for one half and plays well with other? ++
  5. Hard to extend, but excellent documentation? ++
  6. Hard to extend, yet most of the functionality? +
  7. Functionality ok, but outdated? -
  8. Functionality ok, .. but weird (crazy interface, not robust, ...)? --
  9. Library works, but the person who needs to decide is in the state of hybris? ---
  10. Library works, manageable code size, portfolio needs update? ---

Some thoughts ...

If it is something that is small but useful, probably for others, too, then why now write a library and put it on the web. The cost publishing this kind of small libraries decreased, as well as the hurdle for others to tune in (see bitbucket or github). So what's the criteria?

Maybe it should not exactly replicate an existing already known library. If it replicates something existing, it should approach the problem from new angle, or better it should provide a shorter or more condensed* solution.

*/fun

The MYYN
-1 for lack of license consideration.
Stefan Kendall
+7  A: 

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) at StackOverflow, 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. Period.

Dave Jarvis
Thank you for the detailed answer. I agree with most of your points, but I have to pick at your example :-) "If the task is relatively simple (e.g., a MultiValueMap class)" - have you seen Google Collection's MultiMap implementation? It's (surprisingly, perhaps) anything but simple.
ChssPly76
The Licence IMHO would be best if it's non-GPL.
jpartogi
I came to this question because I was debating whether or not I should write my own MultiValueMap class, guess I shouldn't
instanceofTom
Tnay: http://davidjarvis.ca/dave/MultiValueMap.java I admit writing my own; I did not like the ones that I found, and had the time to create something simple and fast.
Dave Jarvis
One thing you didn't mention is documentation. I've several times turned down 3. party libraries because of their (non-existing or bad/outdated) documentation. In any but the most trivial cases, documentation is very important.
nos
Agreed, nos. Updated. Thank you.
Dave Jarvis
A: 

For me this would be a fairly easy answer.

If you need to be cost effective, then it would probably be best to try and find a library/framework that does what you want. If you can't find it, then you will be forced to write it or find a different approach.

If you have the time and find it fun, write one. You will learn a lot along the way and you can give back to the open source community with you killer new bundle of code. If you don't, well, then don't. But if you can't find one, then you have to write it anyway ;)

Personally, if I can justify writing a library, I always opt for that. It's fun, you learn a lot about what you are directing your focus towards, and you have another tool to add to your arsenal and put on your CV.

Tres
Just a note that I agree with this answer but only when you are in a classroom / educational type environment. Generally reinventing the wheel is not a good idea. However too few students have been exposed to doing that invention and do not understand the complexities when they do need to.
wlashell
My answer has multiple parts, one that agrees with you and one that doesn't. However, sometimes you can't avoid creating a new library and sometimes you can. On another side look at PHP frameworks. Almost every single one has re-invented the wheel including the one I've written. Some re-invent it better than others, some do different things than others, but all-in-all, they are created to accomplish the same goal. Generally re-inventing the wheel isn't a good idea due to cost-effectiveness, but other than that reason, if you can make a better wheel, I say go for it.
Tres
+1  A: 

If the functionality is only a small part of the app, or if your needs are the same as everyone else's, then a library is probably the way to go. If you need to consume and output JSON, for example, you can probably knock something together in five minutes to handle your immediate needs. But then you start adding to it, bit by bit. Eventually, you have all the functionality that you would find in any library, but 1) you had to write it yourself and 2) it isn't a robust and well document as what you would find in a library.

If the functionality is a big part of the app, and if your needs aren't exactly the same as everyone else's, then think much more carefully. For example, if you are doing machine learning, you might consider using a package like Weka or Mahout, but these are two very different beasts, and this component is likely to be a significant part of your application. A library in this case could be a hindrance, because your needs might not fit the design parameters of the original authors, and if you attempt to modify it, you will need to worry about a much larger and more complex system than the minimum that you would build yourself.

There's a good article out there talking about sanitizing HTML, and how it was a big part of the app, and something that would need to be heavily tuned, so using an outside library wasn't the best solution, in spite of the fact that there were many libraries out that did exactly what seemed to be called for.

Kevin Peterson