Utils are a blackhole

A dumping ground for code that does not fit anywhere

Ikechi Michael
2 min readMay 3, 2022

--

You’re writing code, following an architecture that works for you.

Everything fits into place until you write a function, and you keep it in a utils folder because it’s not clear where such a function should live. Perhaps such a function is used in multiple places and that is enough justification for it to live in utils, right?

Wrong!

Utils folders start off with good intentions, as a home for misfit functions, variables and constants, until they grow into a behemoth that creates problems like side effects and cyclic dependencies.

Why do we maintain utils folders?

  1. Naming things is really hard.
    Where should encrypt and decrypt function pairs live? Let’s just toss it in utils.
  2. We want our code to be D.R.Y (Don’t repeat yourself)
    Where should a sleep(ms) function, that’s referenced by 11 files, reside? Dunk it in utils.
  3. It’s easy

What can we do instead?

  1. Put some effort into naming things.
    Perhaps encrypt and decrypt could live in a file called encrypt.js or encrypt-decrypt.js? I don’t know 🤷🏻 Go figure! Naming things is hard 😭
  2. D.R.Y is a good compass, but it can be problematic to maintain.
    It might be more important to keep things as close as possible to where they are used, than to take them far away because we do not want to repeat ourselves.
    So a sleep(ms) function could live close as possible to where it is used, be repeated as many times as necessary, and when you can’t bear the repetition anymore, you can place it in a well named file that lives as close to the files that reference it, as possible. Just not in a utils folder.
  3. Realize that Easy is not always good.

A utils folder can make a codebase unbearable.

Shun it! Run from it, and maybe the next maintainer will grunt and say you didn’t do a total shit job.

PS: The constants folder is no different🏃🏻‍♂️

Pictured: utils/*.js

--

--

Ikechi Michael

I’ve learned I don’t know anything. I've also learned that people will pay for what I know. Maybe that's why they never pay.