Keep It Small: The Virtue of Brevity
In the world of software engineering, we often glorify complexity. We build
intricate systems, write sprawling files, and sometimes, we lose our way in the
thicket of our own creations. But I'm here to argue for the opposite: the
profound virtue of keeping things small.
The Tyranny of the Large File
We've all been there. You're tasked with fixing a bug or adding a feature, and
you open up a file that seems to scroll on for eternity. It's a monolithic
beast, a testament to scope creep and forgotten refactors. Even the most
advanced AI assistants, like the impressive Claude Sonnet 3.7, can get bogged
down when faced with such a behemoth. If an AI struggles, what hope does a mere
human have?
Large files are a red flag. They often signal a violation of the
single-responsibility principle, a lack of modularity, and a codebase that's
difficult to reason about. Complexity is the enemy of maintainability. For a
codebase to be truly sustainable, it needs to be simple enough for a human to
read and understand. That means clear, concise code, well-placed comments, and
most importantly, small, focused files.
A Prompt for Sanity
When I encounter a file that has grown beyond a reasonable size, I have a go-to
prompt that I use with my AI assistant to help me break it down. It's a simple,
effective way to start the refactoring process:
I want to refactor the file `table.go`
We are going to read that file, then we are going to reason about the most
logical bits to be in files together, then we are going to create new files
to completely replace the original file. Finally, we will delete the
original file. We should expect errors like things being defined multiple
times until we get to the last step.
This prompt sets a clear, iterative path. It acknowledges that the process will
be messy, with temporary errors, but it keeps the end goal in sight: a cleaner,
more modular, and ultimately simpler codebase.
The Payoff
The benefits of this approach are numerous. Smaller files are easier to read,
easier to test, and easier to reuse. They reduce the cognitive load on
developers, making it easier to onboard new team members and to reason about
the system as a whole. And in the age of AI-assisted development, smaller files
mean lower token usage, which translates to faster responses and lower costs.
So, the next time you're tempted to add just one more function to that
already-long file, take a moment to pause. Ask yourself if there's a better
way. Can you break it down? Can you make it smaller? Your future self, and your
AI assistant, will thank you.