Should I break down my code if it'll only be used once? This is a question I hear often.
The tl;dr answer is Always break down your code.
This is an important key to writing good software. Shift your mentality from writing code for computers to writing code for humans. Knowing the audience of your product (the code), in this case humans, will greatly improve the software you write.
Try to imagine a book with no paragraphs. Pause on this thought and really think about this.
If that idea sounds horrifying and that book sounds tedious to read then imagine that feeling multiplied by ten for a huge function or a massive class that someone has to read.
Don't write a book with no paragraphs or chapters, break your code into readable chunks!
That code may be used only once now. But if you don't break your code down, you are guaranteeing that code can never be reused in the future.
Nobody can predict the future. Give your code the opportunity to be reused later. That function you break out now could become a function you reuse tomorrow.
Maybe all that code you are writing today could have been avoided if your code was written to be reusable yesterday. Reflect on that.
Your project has no tests so why should you write testable code?
Do you also want to ensure your code can never be tested in the future?
Just because a project has no unit testing today doesn't mean that it won't have tests at some point in the future.
Don't be the guy who's code will be untestable when the unit tests do come. Your name is forever on those commits. And the next developers will definitely
git blame you.
- Always break down your code.
- Write code for humans to read. They are your audience, not computers.
- Give your code opportunity to be reused in the future.
- Even if your project has no unit tests now, don't prevent your code from being tested in the future.
- Your name is forever on that commit, you will be judged.
Pet a cat
Follow me on Twitter @joelnet
Camera Photo by Alexander Andrews