Should I break down my code if it'll only be used once?

JavaScript Joel - Sep 10 '18 - - Dev Community

Camera Broken Down

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.

Code is not written for the computer, it is written for humans

If we want to write code for computers we would code in machine language. But instead we choose to use high level languages like JavaScript, which are easier for humans to understand.

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.

Write your code to be readable

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!

But this code is only used once

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.

Think about unit tests

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.

Summary

  • 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.

Read more of my functional programming-ish articles.

Pet a cat

Follow me on Twitter @joelnet

Cheers!

Camera Photo by Alexander Andrews

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player