On NPMs

Peter Ajtai
Solid Digital
Published in
3 min readApr 13, 2018
Using NPMs in node goes along with the general Unix Philosophy of doing one thing and one thing only.

TJ Holowaychuk has a small NPM that converts bytes to gigabytes. I’ve seen people look down on programmers who use micro dependencies that can be replaced by a few lines of code. In fact I’ve read arguments that NPMs like that mean we are forgetting how to program. I think NPMs like that can help us get better at programming.

Source: http://npm.broofa.com/?q=express

Using NPMs in node goes along with the general Unix Philosophy of doing one thing and one thing only.

This philosophy lines up with many other programming paradigms including OOP’s Single Responsibility Principle.

Consistently using NPM for tasks is a pattern that allows leveraging work from others in a responsible fashion. The byte NPM does only one thing, so it is pretty straight forward to code review it. On the other hand, many typical dependencies (Mongoose, Vue.js, etc.) are more daunting to code review.

Additionally conversions are one of those things that is easy to get wrong. It can just be thought of as multiplying by 1024, but where did 1024 come from? How many times do you multiply by 1024?

Using an NPM not only will increase the likelihood of getting the calculation right, but it’ll likely teach you something. If you look at the byte NPM, the magic number of 1024 is only used once, and you can see it’s origin:

A kilobyte is defined as:

1 << 10

In other words take the number 1 — written in binary — and scoot it over to the left ten times. 10000000000 in binary equals 1024 in decimal. Most computers work in binary. So, 1024 doesn’t seem as strange of a choice as it might first appear if you’re familiar with the metric system.

A megabyte is defined as:

1 << 20

A gigabyte is defined as:

1 << 30

Finally a terabyte is defined — with a magic number — as:

((1 << 30) * 1024)

but it could also have been defined as:

((1 << 30) * (1 << 10))

So in fact — conceptually — it is not “multiplying by 1024” it is shifting the number in binary notation.

Transitioning from left shift, to left-pad, dependencies come with risks, but they also give you power. There’s a code snippet in the left-pad article at the top of this post, and that code snippet is now out of date. Left-pad has evolved. Left-pad has tests. If you wrote your own left-pad, would you have written tests? If you would have, is that a good way to spend your time? If you write a code snippet in a project, how do you use it in another project? If you copy pasted ten lines of code into ten projects, what happens when — on the eleventh project — you improve upon those ten lines? With NPM, you just bump your dependency version.

Some of the advantages of using NPMs in node are:

  1. Use a simple established pattern of modularization that allows management of dependencies
  2. Offload work to others (and learn from how they do the work)
  3. Learn from others by seeing how they do things
  4. Use thoroughly tested and thought about functionality (if you pick your NPMs well)

--

--