Don’t repeat yourself

The software industry is full of simple words of wisdom.

Mantras, if you like, always to be obeyed.

Often born of some traumatic experience, days of trying to figure out why it was not working.

Finally, finding the solution, wincing. A new mantra is born.

Balancing this trauma, there is the delight in discovering a new tool or idea.

Wondering how I ever managed before. Before the disappointment sets and and it turns out batteries are not included. Another recursive decsent.

And if a mantra is good, apply to everything, recursively for random kicks and giggles.

The mantra for today is ** Don’t repeat yourself **.

It gets referred to as the DRY principle and can come in lots of

  • Use a list or a dictionary instead of several related variables.

  • Write a function instead of repeating a few lines of code

  • Put a function into a module that you can access more widely

  • Look for common themes in different code.

  • Use a library somebody else provides

Carried to its logical conclusion, this surely ought to be the end of our need to write software. Someone almost always has done something very similar to what you are doing.