Keep it DRY.

As in, don’t repeat yourself.

That is, don’t have copies of the same business logic in the codebase. There is a horrible technique weak developers use known as “copypasta” code, where lines of code are copied from one section and pasted to another. This practice creates a lot of code quickly, but it also creates problems just as quickly.

When a particular piece of logic appears in more than one place, it invites bugs. In the future, another developer with less knowledge of the system will need to make a change that involves this logic. Not realizing it appears in multiple places, the developer makes a change in only one place. Now the code base has two different implementations for what should conceptually be the same bit of logic. As a result, the behavior of the system becomes random and unpredictable.

Keeping code DRY means ensuring that each bit of logic only appears once. This is best done by refactoring down to separate methods and classes. Thankfully, today, there are many tools to detect code duplication, making it easier to avoid this problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: