Why Can't I Just Use This Function? The Struggles with Code Reusability in JS

reggi

Tea Reggi

Posted on October 25, 2023

Why Can't I Just Use This Function? The Struggles with Code Reusability in JS

We've all been there. You know an open-source project does something you need, deeply nested in the dark trenches of a specific file in the repo. You finally, after hunting for a couple minutes you find it. You start to untangle that core functionality and see if there's any feasible way to decouple it from the rest of the project. Is it nested within a rats-nest of classes? You don't need or can't realistically call the parent's parent's parent's class without all the data needed to instantiate those classes. It's all a headache. It shouldn't be this darn hard.

It's not a project maintainers fault that they didn't release this small piece of there project for reuse. However that doesn't take away that it's frustrating the way that code is bundled and distributed.

  1. A whole project might be released as a server or framework. Frameworks like fresh, and astro) both have had things deep within them that I've wanted to reuse, within fresh it's the esbuild configuration, and islands functionality, and within astro it's the rendering of astro files themselves.

  2. A project might be released as a CLI tool, with a whole host of flags and options, .rc files.

  3. A project might be released as a module to run within a specific runtime, NPM, Deno, Bun

  4. A project may or may not be written with typescript, js, mjs, js modules and may or maynot be built within the repo.

This post is mostly just to vent about the sad state of JavaScript. There are more complexities when talking about how people organize their projects, and dependencies, barrel files, front-end frameworks, etc.

I don't know what the right answer is. I just wish that there was a standard format that we could all use that sort of built up functions within a project and allowed us to call them via cli, or server, routes, natively through code. Deno as a runtime is the closest thing to that for me because of how it handles URL imports. It makes it really easy to manage scripts. It also makes it easy to have any file within a repository be the entry point, and it's dependencies, without the need to download the entirety of a project as the dependency, even within this mindset authors need to not use barrel files because it breaks tree-shaking. Overall URL imports are something I don't hear enough people rave about.

💖 💪 🙅 🚩
reggi
Tea Reggi

Posted on October 25, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related