Thomas Reggi's Profile Image

@thomasreggi 🌸

In Defense of Having All Code in a Single File.

November 2, 2023 (Syndicated From dev.to)

I recently wanted to split of a series of scripts I made for a postgres project I was working on into its own cli tool. I started by outlining the tool, I wanted a pgrun bundle, and a pgrun test command. This is what I had after a little bit of development:

.
β”œβ”€β”€ README.md
β”œβ”€β”€ args.ts
β”œβ”€β”€ bundle.ts
β”œβ”€β”€ cli.ts
β”œβ”€β”€ context.ts
β”œβ”€β”€ main.ts
β”œβ”€β”€ options.ts
β”œβ”€β”€ organizer.ts
β”œβ”€β”€ organizer2.ts
β”œβ”€β”€ parse.ts
β”œβ”€β”€ run.sh
β”œβ”€β”€ shake.ts
β”œβ”€β”€ sort.ts
β”œβ”€β”€ sort2.ts
β”œβ”€β”€ sql.ts
└── walk.ts

I had a whole bunch of functions spit into their own files, and it started to be a headache to maintain. I felt every time I made some new thing, I’d isolate it in it’s own file. I’ve vented about files and folders in the past, as well as invented whole new ways to organize typescript projects.

But nothing is as simple as this:

.
β”œβ”€β”€ README.md
β”œβ”€β”€ bin.ts
β”œβ”€β”€ deno.lock
β”œβ”€β”€ mod.ts
β”œβ”€β”€ test.ts
└── types.ts

What’s the shift? Simply having all the functions in one mod.ts file, it’s still under 500 lines. It became a shift slightly but utilizing search and replace within the same file became much easier than digging around the files for the function, and having to deal with import / exports.

This is a gentle reminder to myself to not break things out early on, and keep it simple, keep it all contained in one file if you can, it makes it so much easier to manage.