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:

constisPrimeNumber=(number)=>{returnnumber%2!==0;}

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!