Datasheet

Don’t Repeat Yourself (DRY)
Another important Rails principle is to avoid duplicating information. A tradi-
tional program contains code describing database tables. The code tells the
rest of the program about the structure of the tables. Only after this descrip-
tive code is in place can the rest of the program read data from the database.
But in some sense, the description of a database is redundant. A program can
examine a database and automatically deduce the structure of the database’s
tables. Any descriptive code simply repeats the obvious. “Hey, everyone.
There’s a gorilla in the room. And there’s an
image column in the photos
database table.” So what else is new?
In computer programming, repetition is bad. For one thing, repetition of infor-
mation can lead to errors. If the description of a database is inaccurate, the
program containing the description doesn’t work. (My HMO asks for my
address on every claim form. But my address hasn’t changed in the past ten
years. Occasionally, the folks who process the claim forms copy my address
incorrectly. They mail a reimbursement check to the wrong house. Then I make
ten phone calls to straighten things out. That’s a danger of having more than
one copy of a certain piece of information.)
Aside from the risk of error, the duplication of information means more
work for everyone. With traditional database programming, you must track
every decision carefully. If you add a column to a database table, you must
update the description of the database in the code. The updating can be
time-consuming, and it discourages agility. Also, if each change to a database
table requires you to dive into your code, you’re less likely to make changes.
If you avoid changes, you might not be responding to your customer’s ever-
changing needs.
Let’s Get Going
You can read this chapter’s lofty paragraphs until you develop a throbbing
headache. But the meaning behind these paragraphs might be somewhat elu-
sive. Do you feel different when you switch from C++ or Java to programming
in Ruby? Does Rails really speed up the development cycle? Can you create
an application in the time it takes to find a Starbucks in Manhattan? If you
find these questions intriguing, please read on.
19
Chapter 1: Welcome to the World of Ruby on Rails
05_081204 ch01.qxp 11/30/06 11:09 PM Page 19