DEV Community

Jake Varness
Jake Varness

Posted on

What Nursery Rhymes Teach Us About The DRY Principle

For those of you who don't know, I'm a father! I have a son who was born just last year!

As a result, I've had to listen to a lot (and I mean A LOT) of different nursery rhymes. Some I've never heard before. Some I have, but I never really paid much attention to them.

That is until I heard the song 5 Little Monkeys Jumping On The Bed.

I googled it for you

The Plot

The premise of this nursery rhyme is very simple: there are a total of five ill-behaved monkeys that are jumping on a bed.

During the group's collective effort to jump as high as they can and cause as much ruckus as possible, each monkey takes turns falling off of the bed, causing bodily injury requiring the mother of the monkeys to call a doctor, who subsequently tells the mother "no more monkeys jumping on the bed".

The mother, whilst diligently attending to the monkey who is currently injured, neglects the fact that many other monkeys are also jumping on the same bed. As each monkey falls off, the mother tends to each child individually, one-by-one, until there aren't any monkeys jumping on the bed.

The Breakdown

There are multiple things at play here:

First, each monkey is performing a similar action, and each monkey subsequently fails in a similar fashion as well.

Second, rather than instructing all of the monkeys to behave, the mother decides to only focus on the monkeys that have demonstrated the problem until all are tamed.

Finally, once each are tamed, the monkeys each stop jumping on the bed. However, because each monkey is still sentient and each have a disposition to poor behavior, each monkey is likely to perform actions that could cause further harm to themselves and those around them.

Possible Solutions

We can't solve the mother's problem of having 5 monkeys that enjoy getting themselves into trouble, and try as we might to correct their behavior, they are still prone to misbehaving.

We also can't mitigate this issue by consolidating the monkeys and reducing them to 1 monkey, so unfortunately the mother might just have to live with her 5 little monkeys until they move out of the house.

For us programmers, however, reducing the amount of monkeys in our code is very possible!

The DRY Principle

Contrary to what the name might suggest, the DRY Principle has nothing to do with how wet water your code is.

DRY is an acronym that stands for Don't Repeat Yourself. This stems from the idea that your codebase shouldn't have duplicated code in it.

But why not? I mean, if the code works for what you need it for, why not just copy/paste it wherever you need it?

For the same reason that the mother should have stopped all her little monkeys from jumping off the bed: you're just going to keep getting hurt by it.

When you duplicate code, you duplicate all previous issues the code had, and gain none of the future benefits that future code changes can provide. All you get when you duplicate code is a monkey jumping on the bed that is your source code.

Take this code for example:

const isPrimeNumber = (number) => {
    return number % 2 !== 0;
}
Enter fullscreen mode Exit fullscreen mode

Obviously this JavaScript code is problematic for a number of reasons: first, it assumes that if a number is not evenly divisible by 2 that it's a prime number. Second, it assumes that the variable passed in is always some kind of numeric value that you can perform integer division on.

Other engineers who see this code might decide that to keep the same behavior throughout the rest of the system, the code needs to be duplicated to other places.

Now that this function exists in multiple places within the codebase, the problem has spread to other places in the system. The monkey that used to be isolated has now multiplied and they are running rampant throughout the system.

What would have been better is if this function were extracted into it's own reusable component and updated to have the correct functionality in later versions. That would have tamed the monkey from the start!

Duplicating later versions of this function, however, does not remedy this problem. Duplication also causes a lot of manual intervention and unnecessary work to maintain and update in the future. Every time the original function is updated, the clones of it would need to be updated as well. If your code is going to have a monkey in it, ensure there's only one monkey: don't repeat your monkeys.

Conclusion

I hope that this article has been very informative and enlightening. As silly as this example is, it just goes to show you that you can find good programming advice and lessons in a lot of different places!

Discussion (5)

Collapse
cat profile image
Cat

This is absolutely amazing and well-written. It gave me a clear visual story to follow and recall when writing code.

Also, congrats on becoming a pops! Shall we send you grilling accessories for father's day? lol

Collapse
jvarness profile image
Jake Varness Author

I'm glad you enjoyed it!

Grilling accessories? That's a fantastic idea! I think Ben Halpern needs to start selling DEV spatulas!

Collapse
maccabee profile image
Maccabee • Edited

As a father I 100% percent endorse this. Though sometimes the monkeys can differ slightly, like my 2.

Collapse
edoxtator profile image
Doc • Edited

Is it necessary for:

That is until I heard the song 5 Little Monkeys Jumping On The Bed.

I googled it for you

When you could have just pasted the lyrics in the post?

I lost interest as soon as I got the condescending "I googled it for you".

Collapse
jvarness profile image
Jake Varness Author

I was mostly trying to save people time so that they didn't have to google it themselves.

Plus, I think lmgtfy is a hilarious website.