DEV Community

Tea Reggi
Tea Reggi

Posted on

In Defense of Having All Code in a Single File.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (1)

Collapse
 
pavelee profile image
Paweł Ciosek

It's cool approach, I like your line of code rules. ❤️

IMO any rules is good if it works for you and team can be consistent in it.

Have you thought about creating your lint rule?