Keep related utilities in a single file - a podcast by Kent C. Dodds

from 2021-05-28T18:13:25

:: ::

Hello there friends, so I was thinking this morning about utilities and you
know these little utility functions that we write and we put it in like autils modular or something and sometimes when we're running these we want
to split things out to make it just.Easier to manage or something and andit's just kind of a natural thing at least for me to want to split things
out into individual files, so each utility is in its own file, you know, ifit's just a single line or something.
I typically just keep it on one but if it's kind of complicated or maybe
there are group of utilities that make a lot of sense together. I just feelnaturally inclined to move those into a separate file. But what I've found
is in my experience is that you're often better off just leaving utilities
all in the same file and thenMaybe using code comments or something toseparate different parts of that file because what what I found happensover and over again is one utility depends on another and that utility
depends on this other utility which depends on the first one and so youhave like a cyclic dependency or cyclical dependency sort of thing and so
when you try to start pulling things out into different utility or intodifferent files, now you have to extract the other files.
Or other utilities into separate files as well ultimately leading tobasically a lot of files that have just a couple lines of utilities. And I
definitely don't see that as being any simpler. And in fact, it can be even
more complex when you start adding type script and now you have types thatyou need to extract it to different files so you can use them together.
So, but there is a reason that we like to keep our files shorter and that's
because when when you have a really big utility that can be reallycomplicated. I,Worked on a a backbone view that was like 3,000 lines long
once and it was not fun and I did not like having such a big file, but I've
been thinking about this and I think that the problem isn't necessarily the
size of the file the problem is the size of the the individual component.And so and just the number of concerns it was mixing. That was what made it
really difficult. And so yeah, the the length of the file that doesn't
really matter all that much. It's the number of mixed concerns that makes
things really difficult. So, I would encourage you to.Think twice before extracting utility files or your utility functions into
individual files and maybe first try to organize that file a little bit tomake it easier to manage. I hope that's helpful and interesting have a nice
day.

Further episodes of 3 Minutes with Kent

Further podcasts by Kent C. Dodds

Website of Kent C. Dodds