Utils are a blackhole
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 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?
- Naming things is really hard.
decryptfunction pairs live? Let’s just toss it in
- 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
- It’s easy
What can we do instead?
- Put some effort into naming things.
decryptcould live in a file called
encrypt-decrypt.js? I don’t know 🤷🏻 Go figure! Naming things is hard 😭
- 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.
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
- 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.
constants folder is no different🏃🏻♂️