Write it once

From Jonathan Gardner's Tech Wiki
Jump to: navigation, search

Introduction

You may hear the "DRY" principle, meaning, "Don't Repeat Yourself". I call it "Write it once". Let's explore what this means and why it is good.

What does "Write it Once" mean?

"Write it Once" means exactly what it says. If you have an idea, write it once. If you want to refer back to that idea, point to it, but don't copy-and-paste.

Here's a good example. Let's print out the numbers 1 through 10. (Python code below.)

print "1"
print "2"
print "3"
print "4"
print "5"
print "6"
print "7"
print "8"
print "9"
print "10"

Note the following:

  • I wrote "print" 10 times.
  • I wrote double-quotes 20 times.
  • I hit "enter" ten times.
  • I also wrote out a simple series---the numbers 1 through 10.

The "Write it Once" says to change the code above to:

for i in range(1, 11):
    print i

Now look what I did:

  • Condensed 10 "print"s to 1 "print"
  • Condensed 20 double-quotes to nothing
  • Expressed the range of numbers as human-readable code rather than the literal series.

There are many, many handy tools to help you "write it once" in every programming language.

How do you tell if you have code that can be simplified? Use this process of analysis.

  • Does any code appear cut-and-paste from somewhere else? If so, replace it with a function or wrap it with control flow statements.
  • If you cut-and-pasted and then modified the resulting code, can you describe what you just did? If so, write a program that does it for you, and use that instead. Programs are very good at writing programs, so take advantage of that. (And avoid languages that make that task difficult.)
  • Are you coding from memory? Then try to write down the process in code and reuse it. Free up that valuable memory for something computer's can't do because you haven't figured out how to explain it yet.
  • If you are telling someone how to do something, and the explanation involves anything more than "Call this method with these parameters", then you can reduce the task down to a single method call that your other bits of code should rely on.

Why Write it Once?

There are many reasons why. I'll list a few.

  • You have to type a lot less to get your code to work, saving typing time.
  • The code is much simpler and easier to read, since there is less of it. Consider memorizing 100 bottles of beer on the wall, the literal version, versus the instructions to create the song from scratch.
  • People will be more likely to look at and analyze your code if there is less of it. It's easier to test 1 thing than 10, so try to make as little as possible.
  • You can reuse work you've already done. This includes the task of coding and compiling, as well as testing and real-world experience.
  • You will begin the process of building on the shoulders of giants, even if those giant's shoulders were your own. In other words, you will actually progress as a programmer, rather than re-doing the same task over and over again.