Question

I am in a situation where I can use an open source JavaScript plugin to fulfill a task. But when I tried to use it,I found myself I have to redesign lot of things of what I already have done,and it adds a certain complexity, in my humble opinion, to the project. Whereas I can achieve the same task with a clean code I can craft myself, and without needing to change what I have done so far.

Should you opt for a library anyway in this situation (like for the sake of better quality code?)

Was it helpful?

Solution

As an engineer, perhaps it is suitable to think of this as an optimization problem. Naturally we must have an optimization goal. A common one in this sort of situation would be to minimize Total Cost of Ownership.

If you believe adding the third party component will save cost in the long run, you should use it. If you don't, you shouldn't. Make sure you consider the cost of ongoing maintenance (for example, when a new version of O/S is released, or a security flaw is found, or some new W3C specification is released).

For many trivial problems, it will be lower cost to grow your own, but for moderately complex problems outside your organization's core competency, it will often make sense to go third party.

There are other goals to consider too (e.g. risk) but TCO is the big one.

OTHER TIPS

Bill Gates once famously said:

"Measuring programming progress by lines of code is like measuring aircraft building progress by weight."

This quote comes to mind because the same could ultimately be said for the number of libraries. As a rule, I don't use libraries unless:

  1. There's no other way of getting it done. Doing without would no longer be economically viable to produce the product on time and on budget.
  2. It would save me a significant chunk of time as I would require many of the features of said library
  3. Library is well-used and any potential problems I might have would be well-documented.

Ideally all three conditions are met, but I'd settle for any two. Bottom line is you shouldn't be adding a library to your program unless it serves a purpose. If you have to ask what that purpose is, you probably shouldn't be adding it to your program. The code quality of your program therefore benefits because it elegantly calls upon each library without being weighed down by the need to necessarily rewrite libraries inside your program.

Good luck!

(Note: The original question was: Does the number of libraries improve code quality?)

You can probably answer that one for yourself: No, of course the mere fact of using libraries doesn't improve your code. If it did, it would be easy to write great code for everything with no effort.

What people mean when they recommend reuse over roll-your-own is that code in a well-known library is probably more correct, efficient and/or usable than what you would yourself come up with, simply because the authors have spent much more time on one particular area of functionality than you (with your deadline for the entire project) can afford.

But that's only a trend, not a law. Certainly there can be libraries that aren't as useful to use as roll-your-own would be. Often this happens when the library actually does much more than what you need, and does it in a way that would force you to adapt your own code base to their conventions much more than is reasonable. It looks as if this is exactly what you've found in this instance.

While using the right libraries can save you a lot of work, there is also a lot of hidden cost:

  • Libraries need to be kept up to date. You regularly need to check if they got updates (which might be security-relevant!) and apply them. Each library update might potentially break something in your application. That means you need to perform a complete integration test afterwards. So every library your project depends on increases the long-term maintenance overhead.
  • Some Javascript libraries are so powerful and use such unique patterns that people start to perceive them as separate technical areas of expertise. So each additional library you add might scare away developers who do not feel confident editing code which relies on a framework they are not familiar with. Hiring new maintenance programmers who are familiar with all the libraries you use might become challenging.
  • Adding a library to your website increases loading-times, because the user needs to load the whole library, even if you only use a small part of it. Some popular libraries allow you to download custom builds with only the functionality you need, but even then you will usually still include a lot of code which will never run (or even worse: code which does run, but doesn't do anything useful, because it just prepares data-structures for functionality you don't use).

So before you add another dependency to your project to include something you could just as well write yourself, do a cost/benefit analysis.

This needn't be a binary decision: Either only use an OSS library, or program a new solution from scratch. Another option may be to re-use parts of the library, if the licence allows it.

For example, in my field (numerical software) a library can have fine core modules, and some specialised modules that I'm only 80% happy with. So I'd use the core modules and write a wrapper for the specialised modules. Or I may develop my own specialised modules by using the design and code of the OSS modules. The hardest, algorithmic bits usually get re-used from those, with only scaffolding code modified. I may also clean up some of the original code. This has proven a good learning experience and a time-saver, compared to development from scratch.

If someone has done the work for you already, of course you should use it.

The exception to the rule is javascript. Where they will have imported a dozen other libraries (obsolete versions of course) to add the language features they want to use and then done the work for you.

Pick your framework and stay within it. If the library works with your framework or plain js, fine. But if it needs a different framework, look for another option.

Libraries and when to use them is a complicated decision.

On the one hand you have well tested, almost standard things (In my field, FFTW for example falls into this category, or something like libsndfile), which are generally acknowledged to just work, and have been standard things for the last 20 years that everyone uses.

On the other hand you have random stuff from github, with no test suite and only about 1 maintainer, generally why bother?

The acid test for me is firstly does the library fit into my architecture (Sometimes, if you know you want to use a given library you end up designing around that), and do I think I am going to wind up debugging someone elses library code? A good proxy for the second question is "Is there an automated test suite and what is the documentation like?".

A little debugging is not a major problem, but at that point the library code starts to count against my own code size from a maintenance perspective (More so if my fixes cannot be pushed to upstream for some reason).

I would also differentiate between libraries and frameworks, for all that the distinction is sometimes not that clear cut, frameworks in my (small core, DSP heavy) world tend to be a pain in the arse, especially if you are trying to merge more then one or do something slightly outside the lines, libraries are sometimes useful. I am aware that this is seen very differently in the web dev scene.

End of the day it is a decision that comes down to taste and experience, and even the experienced sometimes pick poorly, still at least with a library, you can always tear it out and write your own implementation if it gets too annoying.

Decisions, Decisions....

Licensed under: CC-BY-SA with attribution
scroll top