Reframing the DRY principle as DRUM

Example of ‘repeated’ (similar) code
  • Produce an account statement
  • Produce a payment overdue notice
  1. Retrieve the account based on the ID in the command data
  2. Initialise a variable called $accountTotal
  3. Loop though all the items in the account and increment the $accountTotal by the item’s total
  4. Generate a PDF by passing the template path, account and account total to a PDF generator
Example of an attempt to remove all ‘repeated’ (similar) code
  1. Moved the block of code that was repeated in the previous example into an extendable class
  2. Made the single part that differed (the PDF template path) into a variable
  3. Extended that class twice
  4. In both overridden classes: call the parent class, passing in the template path
  • We’d have to introduce more exceptions which means a more fragile design and harder to understand logic
  • The refactor would affect both handlers— so the scope of the change would be larger — meaning more potential for bugs and more broken unit tests
Example of ‘repeated’ (similar) code but no repeated implementations of meaning
  • When the DRY principle comes to mind think in terms of what exactly it is you’re trying to avoid repeating. Is it universal meaning? Or is it something superficial?
  • Ask yourself: when one of these things changes, should they all change in all circumstances?
  • Copy and pasting code isn’t always a bad option 😈

--

--

Software engineer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store