Utils are a blackhole
A dumping ground for code that does not fit anywhere
--
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?
- Naming things is really hard.
Where shouldencrypt
anddecrypt
function pairs live? Let’s just toss it inutils
. - We want our code to be D.R.Y (Don’t repeat yourself)
Where should asleep(ms)
function, that’s referenced by 11 files, reside? Dunk it inutils
. - It’s easy
What can we do instead?
- Put some effort into naming things.
Perhapsencrypt
anddecrypt
could live in a file calledencrypt.js
orencrypt-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.
So asleep(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 autils
folder. - 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🏃🏻♂️