August 18th, 2024

Micro-libraries need to die already

The article critiques micro-libraries for introducing complexity and security risks, advocating for developers to use simple utility functions instead, which can reduce dependency issues and improve code efficiency.

Read original articleLink Icon
Micro-libraries need to die already

the safety of established libraries, even when they may not be the best solution. This reliance on micro-libraries can lead to unnecessary complexity and increased risk in software development. The author argues that micro-libraries often do not provide significant benefits compared to the costs they incur, such as poor fit for specific problems, potential security vulnerabilities, and bloated codebases. Instead of using these tiny libraries, developers should consider copying and pasting simple utility functions directly into their code. This approach minimizes risks associated with third-party dependencies and avoids the pitfalls of frequent updates and transitive dependencies. The article emphasizes that the programming community should rethink the use of micro-libraries and recognize that many trivial functionalities can be effectively handled with straightforward code snippets.

- Micro-libraries often introduce more problems than they solve, including security risks and performance issues.

- Copy-pasting simple utility functions can be a safer and more efficient alternative to using micro-libraries.

- The reliance on libraries may stem from a fear of making mistakes, leading to unnecessary complexity in codebases.

- Many common functionalities can be implemented with simple one-liners, negating the need for external libraries.

- Developers should critically assess the trade-offs of using libraries versus writing their own code.

Link Icon 42 comments
By @ristos - 3 months
Micro-libraries are really good actually, they're highly modular, self-contained code, often making it really easy to understand what's going on.

Another advantage is that because they're so minimal and self-contained, they're often "completed", because they achieved what they set out to do. So there's no need to continually patch it for security updates, or at least you need to do it less often, and it's less likely that you'll be dealing with breaking changes.

The UNIX philosophy is also build on the idea of small programs, just like micro-libraries, of doing one thing and one thing well, and composing those things to make larger things.

I would argue the problem is how dependencies in general are added to projects, which the blog author pointed out with left-pad. Copy-paste works, but I would argue the best way is to fork the libraries and add submodules to your project. Then if you want to pull a new version of the library, you can update the fork and review the changes. It's an explicit approach to managing it that can prevent a lot of pitfalls like malicious actors, breaking changes leading to bugs, etc.

By @oftenwrong - 3 months
The primary cause of the left-pad incident was that left-pad was removed from the npm registry. Many libraries depended on left-pad. The same could have occurred with any popular library, whether micro or not.

To reformulate the statement made in the intro of this post: "maybe it’s not a great idea to outsource _any critical_ functionality to random people on the internet."

It has long been a standard, best practice in software engineering to ensure dependencies are stored in and made available from first-party sources. For example, this could mean maintaining an internal registry mirror that permanently stores any dependencies that are fetched. It could also be done by vendoring dependencies. The main point is to take proactive steps to ensure your dependencies will always be there when you need them, and to not blindly trust a third-party to always be there to give your dependencies to you.

By @xg15 - 3 months
Micro libraries are worse than no libraries at all - but I maintain they are still better than gargantuan "frameworks" or everything-but-the-kitching-sink "util"/"commons" packages, where you end up only using a tiny fraction of the functionality but have to deal with the maintenance cost and attack surface of the whole thing.

If you're particularly unlucky, the unused functionality pulls in transitive dependencies of its own - and you end up with libraries in your dependency tree that your code is literally not using at all.

If you're even more unlucky, those "dead code" libraries will install their own event handlers or timers during load or will be picked up by some framework autodiscovery mechanism - and will actually execute some code at runtime, just not any code that provides anything useful to the project. I think an apt name for this would be "undead code". (The examples I have seem were from java frameworks like Spring and from webapps with too many autowired request filters, so I do hope that is no such an issue in JS yet)

By @franciscop - 3 months
Seems a lot like the classic "I put only a couple of the strong advantages and enumerate everything I could think about as disadvantage". While I'm bias (I've done a bunch of these micro-libraries myself), there's more reasons I/OSS devs do them! To name other advantages (as a dev consuming them):

- Documentation: they are usually well documented, at least a lot better than your average internal piece of code.

- Portability: you learn it once and can use it in many projects, a lot easier than potentially copy/pasting a bunch of files from project to project (I used to do that and ugh what a nightmare it became!).

- Semi-standard: everyone in the team is on the same page about how something works. This works on top of the previous two TBF, but is distinct as well e.g. if you use Axios, 50% of front-end devs will already know how to use it (edit: removed express since it's arguably not micro though).

- Plugins: now with a single "source" other parties or yourself can also write plugins that will work well together. You don't need to do it all yourself.

- Bugs! When there are bugs, now you have two distinct "entities" that have strong motivation to fix the bugs: you+your company, and the dev/company supporting the project. Linus's eyeballs and all (yes, this has a negative side, but those are also covered in the cons in the article already!).

- Bugs 2: when you happen upon a bug, a 3rd party might've already found a bug and fixed it or offered an alternative solution! In fact I just did that today [1]

That said, I do have some projects where I explicitly recommend to copy/paste the code straight into your project, e.g. https://www.npmjs.com/package/nocolor (you can still install it though).

[1] https://github.com/umami-software/node/issues/1#issuecomment...

By @qwerty456127 - 3 months
> Micro-libraries should never be used. They should either be copy-pasted into your codebase, or not used at all.

I would prefer them to be built straight in the languages.

By @flysand7 - 3 months
I was about to jump into the comment section and say something along the lines of "but no one really thinks they're actually good, right?", only to see the top comment arguing they're good.
By @userbinator - 3 months
and because it updates fairly frequently

I fail to comprehend how a single-function-library called "isNumber" even needs updating, much less "fairly frequently".

The debate around third-party code vs. self-developed is eternal. IMHO if you think you can do better than existing solutions for your use-case, then self-developed is the obvious choice. If you don't, then use third-party. This of course says a lot about those who need to rely on trivial libraries.

By @shiroiushi - 3 months
Until I read the comments here, I thought from the title that this was about those small neighborhood "libraries" that are basically a box the size of a very large birdhouse, mounted on a post, with a bunch of donated books inside that passersby are free to borrow. I was really wondering why someone would have a problem with these, unless they work for a book publisher.
By @PhilipRoman - 3 months
Micro libraries are fine (well... not really), the problem starts when each of those depends on 10 more "micro" libraries and so forth. The branching factor quickly leads to bloat. Libraries have a duty to minimize their footprint in ways that applications do not.
By @KaiserPro - 3 months
I suspect this might be seen as trolling, but why isn't there a standard lib of stuff like this?

surely it can't be beyond the wit of programming kind to have a standard lib, or even layers of standard lib for Node?

What is the argument for not having a standard lib, apart from download speed?

By @edwinjm - 3 months
If your only examples are leftPad and isNumber, I can’t take this article seriously. There are so many really useful micro libraries.
By @AndyKelley - 3 months
Missing benefit: Libraries are a way to distribute and share the costs of labor, resulting in a more efficient ecosystem.

This doesn't apply to micro-libraries, but it looks like that cost/benefit list is intended to cover libraries in general.

By @Lws803 - 3 months
I would argue that "They should either be copy-pasted into your codebase" would cause more code liabilities and maintenance required further down the line. I've personally seen codebases with a ton of custom code, copy-pasted code, inspired implementations before and it was horrible to get them up to speed with the latest functionality / best practices. I agree that having too many micro-libraries might not be beneficial though, but perhaps look for larger, more well-established libraries that encompasses those functionalities :)
By @Joker_vD - 3 months
> One breaking change simply upgraded the minimum supported Node version from 0.10.0 to 0.12.0 and changed nothing else.

Well, that's a proper use of SemVer, not sure why you put it against the library's author. I've personally been burned enough times by libraries that for some reason think that literally being unable to compile them is somehow a backwards-compatible change, so it's refreshing to see that some people actually understand that.

By @wakawaka28 - 3 months
There's nothing especially wrong with small libraries if you carefully manage them and don't allow for supply chain attacks. I don't think updates are a serious concern compared to not using a library, because your own code could easily have vulnerabilities too. It is harder to update lots of small libraries versus one big library, but you pick your battle.
By @gjsman-1000 - 3 months
In my Laravel projects, there are a few packages of much more niche/hobbyist origin without corporate backing, some haven’t been updated for a while, and others are perfectly fine and don’t need much maintenance.

Normally, packages are listed in my composer.json and stored in vendor/. For those packages, I created a separate folder called vendor_private/ which is part of my Git tree, put copies of these weird little packages in it, and set up my composer.json to consider that folder a repository.

Works like a charm. My big important packages are still upstream. I can customize the little ones as needed to fit better, or have better code, and not worry about them going unmaintained. It’s also way quicker than copying the files individually out of the package and into the right places (along with updating Namespaces, configuration, etc.) Once in a while, I’ll go back and see if anything worthwhile has changed upstream - and so far, it never has.

By @mirekrusin - 3 months
Personally I prefer sharing one level up from function to conceptual module, ie. instead of "left-pad" function, "string" module, ie. a bit like this [0] (`${authorshipDisclaimer}`).

I'm also an advocate, against crowd, of qualified imports as they help with refactoring (renames are propagated, especially in monorepos), readability/reviews (functions are qualified, you know where they're coming from) and overall coding experience – qualified module name followed by dot gives good autocompletion, imports look neat in larger projects etc. The codebase written like this resembles extended standard library. It also helps with solving problems by encouraging first principle thinking, bottom up coding that produces auditable codebase with shallow external dependencies etc.

[0] https://github.com/preludejs

By @morningsam - 3 months
These arguments aren't very convincing IMHO.

Using SNS as an example when it's neither micro nor a library but a service (and a huge abstraction over native push notifications, whereas most micro-libraries provide simple utilities that aren't very abstract), saying that complex libraries are harder to audit and hence a security risk (which should be a point in favor of micro-libraries that are small enough to audit in minutes), saying libraries might have large footprints (which is surely another reason to go for micro-libraries over all-you-could-possibly-need-libraries), saying transitive dependencies are bad, (yet again, this points towards an advantage of micro-libraries, which are less likely to have many dependencies), ... I don't know.

By @tptacek - 3 months
I think the JS library ecosystem is a debacle, but there's really only one point in this post that grabbed me:

"Would future updates be useful? No. The library is so simple that any change to the logic would be breaking, and it is already clear that there are no bugs."

Maybe what you want is a library ecosystem where things can be marked "this will never change". Something crazy happens and you actually need to update "is-number"? Rename it.

Of course, you can simulate that with a single large omnibus dependency that everyone can trust that pulls all these silly micro-libraries in verbatim.

By @unstable - 3 months
> You can write isNumber(foo) instead of typeof foo === "number".

Indeed you can, but it depends what isNumber does. This is more like what it should do IMO:

function isNumber( foo ) { return ( (typeof foo === "number") && (foo == foo)) || ((typeof foo === 'object') && (foo instanceof Number) ); }

And that is I think the value of micro libs, at least in JS, you don't want to think about all the edge cases when you only want to check if something is a Number.

By @rc_kas - 3 months
the entire nodejs ecosystem needs to die. You all are just keeping it alive.
By @n0tank3sh - 3 months
It really depends on the case. Some folks use the left-pad library for aligning, which can be done in 10 minutes. In C++, we have header libraries for thread pools, etc. I don't think implementing a fully functional thread pool with waiting and other features is an easy task. In conclusion, it really depends on the situation.
By @qudat - 3 months
Partially disagree, JS has unique features that require small libraries: https://bower.sh/my-love-letter-to-front-end-web-development

However, if I can inline a small function, I will, so in that sense I agree.

By @replete - 3 months
Not checking dependency updates need to die already. Not choosing better dependencies needs to die already.
By @TacticalCoder - 3 months
Thought experiment: if a LLM can correctly produce the code for a micro-library like, say, leftpad... Should you call leftpad as a dependency or should you have the LLM generate that leftpad function for you?

And if the LLM ain't good enough to write leftpad, how can I trust it to write anything at all?

By @edfletcher_t137 - 3 months
Where do you draw the line: is 10 lines "micro"? 50? 100? It is never quantified within, yet the very click-bait-y title relies on the term. How many bugs can hide in 50 lines or 100? And you really want to copy-paste that code at a static point in time?!
By @Cheezmeister - 3 months
At no point does this article attempt to define the term "micro-library".

Perhaps we should start there.

By @statictype - 3 months
I have found that micro libraries are being replaced by ChatGPT generated functions.
By @LordHeini - 3 months
I think discussions like this mostly miss the point.

Obviously you want basic, stable and well documented functionality in your programming language.

But JavaScript does simply not have it. So how do you solve this dilemma?

1) the everything is an import way: use NPM and create a dependency hell from hell (requires Satan) made by Lucifer (same as Satan but different) using lava with fire (requires node v <= 9.42.0815) and heat (deprecated) requiring brimstone (only node v > 10.23) with a cyclic dependency on the Devil (incompatible with Satan).

2) the Golang way: copy paste ALL the things, only for your co worker to copy paste all the things again, only for your co worker to copy paste all the tings again, only for your...

Way 1 wastes your time when it breaks (sooner than later) but is necessary for non trivial functionality. Way 2 works only for trivial packages so choose your poison.

JavaScript (apart from not being a good programming language in general) is sorely missing a std lib.

One could argue that having a bad std lib is even even worse (PHP anyone?) but it is really hard to decide.

Sadly JavaScript is just unfit for the purpose it is being used for.

By @kazinator - 3 months
This seems better articulated like this:

Applications should never have trivial, tiny libraries as moving-target external dependencies.

If you must use a small library, bring it into the program.

By @stuaxo - 3 months
The bigger principle is that atomising everything into small parts separated from their context obfuscated them and the bigger picture.
By @edwinjm - 3 months
So, everybody can contribute an npm package.

The advantage: - everybody can contribute an npm package

The disadvantage: - everybody can contribute an npm package

By @mkoubaa - 3 months
Why don't people just copypasta the library into their codebase with the license header intact and keep it out of npm
By @DonHopkins - 3 months
>Micro-libraries should never be used

Passive voice. WHO should never use micro-libraries?

By @ptman - 3 months
A little copying is better than a little dependency.
By @gerdesj - 3 months
"The library may be a bad fit for your problem."

How is this the fault of the library? You chose the wrong one!

"This often cancels out the primary benefit of libraries. No, you don’t have to write the code, but you do have to adapt your problem to fit the library"

You evaluated the library, found is unsuitable and yet, it is somehow their fault.

Why on earth would you project your own failures on to someone else's code? You do you!

By @crabmusket - 3 months
While I mainly agree with the author's substantive point, though I find some of the ways it's presented in this post not entirely convincing or fair, I am interested that someone else has identified this:

> I have talked a lot about the costs of libraries, and I do hope people are more cautious about them. But there’s one factor I left out from my previous discussion. I think there’s one more reason why people use libraries: fear.

> Programmers are afraid of causing bugs. Afraid of making mistakes. Afraid of missing edge cases. Afraid that they won’t be able to understand how things work. In their fear they fall back on libraries. “Thank goodness someone else has solved the problem; surely I never would have been able to.”

I think this is true, but why does the JS ecosystem seem to have "more fear" than for example the Python ecosystem?

I wrote about this a while ago. I think that actually JS does (or did) cause more fear in its developers than other programming languages. I described it as paranoia, a more insidious uncertainty.

Quoting myself[1]:

> There are probably many contributing factors that have shaped NPM into what it is today. However, I assert that the underlying reason for the bizarre profusion of tiny, absurd-seeming one-liner packages on NPM is paranoia, caused by a unique combination of factors.

> Three factors have caused a widespread cultural paranoia among JavaScript developers. This has been inculcated over years. These factors are: JavaScript's weak dynamic type system; the diversity of runtimes JavaScript targets; and the physics of deploying software on the web.

...

> Over the years there has been rapid evolution in both frontend frameworks and backend JavaScript, high turnover in bundlers and best-practises. This has metastasized into a culture of uncertainty, an air of paranoia, and an extreme profusion of small packages. Reinventing the wheel can sometimes be good - but would you really bother doing it if you had to learn all the arcane bullshit of browser evolution, IE8 compatibility, implementation bugs, etc. ad infinitum?

> And it's not just that you don't understand how things work now, or how they used to work - but that they'll change in the future!

[1] https://listed.to/@crabmusket/14061/javascript-s-ecosystem-i...

By @nalgeon - 3 months
The title of the article is "Micro-libraries need to die already". Renaming the submission to "Micro-libraries should never be used" is pathetic, Daniel. I'm not surprised though.
By @joshmarinacci - 3 months
TL;DR most micro libraries should be gists