BRIEFS logo BRIEFS

Keep related utilities in a single file

05/28/2021 , 2m, 52s

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 a utils 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 and it'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, if it'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 feel naturally 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 to separate different parts of that file because what what I found happens over 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 you have like a cyclic dependency or cyclical dependency sort of thing and so when you try to start pulling things out into different utility or into different files, now you have to extract the other files.

Or other utilities into separate files as well ultimately leading to basically 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 that you 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 really complicated. 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 to make it easier to manage. I hope that's helpful and interesting have a nice day.