Don’t just install things

We’ve all been there. That one weird bug that seems to signal something systematic is wrong with your app. That boilerplate code that makes something more tedious than it ought to be. That inscrutable thing that makes you want to stop trying and *gasp* search StackOverflow or read a blog post for an answer. And while sometimes the answers you’ll find there might tell you how to better use what tool or framework you have at hand, occasionally they will suggest you simply install some tool, which will do the trick without any consequences.

Don’t. Just don’t. Here’s why.

Because YAGNI

YAGNI (short for “You aren’t gonna need it”) is the first reason to stop and think.

  • Identify the problem in words. What do you want to do? What are the inputs and outputs that work for you?
  • For JS, is this for web, or for some CLI script or server-side problem?
  • Will you need a polyfill?
  • Is cross browser support a concern?

Here are some followup questions to ask yourself:

  • Is the problem I am trying to solve likely to be used in multiple places by others?
  • Is the problem I am trying to solve complex?
  • Is the problem I am trying to solve something that is not doable with what we already have in the project?

If the answer is yes to all of the above, congrats; you may need to install a new package! Most likely, though, there’s a few “no”s in there. Let’s look at an example, shall we?

“I need to merge two objects”

This is a common-enough problem. Say you have object A and object B, and you want object C to be equal to what’s in A and B … somehow (more on that in a bit). How would you do it? Depending on your level of experience, several things might come to mind. If we are talking modern JavaScript, you may think, “Cool I’ll just use Object.assign. Easy-peasy.” If you don’t know Object.assign exists, what would you do instead?

There are those who would search the internet for help with this, and may come across an article that points them to LoDash, an otherwise delightful library full of useful functions. So, how about we just install it?

NO.

  • Is merging two objects in JavaScript complex? NO.

We exit the list of questions. If this is the ONLY thing you need to do, there is no sense tying your whole project to such a large collection of functionality. Further, this includes such arguments like, “but we might use other stuff in LoDash eventually” or “My code may have bugs that aren’t in LoDash” etc. Your whole project could have bugs not in LoDash!

The better approach is to find a way to accomplish the goal that doesn’t require a dependency when you are sure you only need this particular functionality. Read up on the standard library in JavaScript. Browse or search MDN. Ask a knowledgeable colleague. Don’t just install things.

Ultimately this approach leads to knowledge… something that can be retained and shared. At some point, your answers change from “I don’t know; maybe search StackOverflow” or “Oh yeah there’s probably a package for that” to “You can use Object.assign. Here’s a link to the docs. Let me know how that works for you.”

Much better.


There’s one side note worth mentioning. Packages often have behaviors and functionality that you absolutely will NOT use, but include the overhead of existing. They often may include behavior that aren’t quite right for your use case as well. Imagine choosing something that copies Object A into B, but overwrites things in B in a way you don’t want. Using that troublesome methodology (“search google or stackoverflow for an answer”) can lead to even more complexity, or potentially installing even MORE packages to work around those things. Yuck.

Consider those questions above, which help narrow all the possible solutions down to a minimum set.

Interesting Readings

Leave a comment