Graham Knapp

Two's company, three's a crowd

written by Graham Knapp on 2025-01-20

My version of YAGNI - "You Ain't Gonna Need It" - is a rule of three: only spend time refactoring your code when you have repeated yourself 3 times. Don't aim for truly DRY code devoid of all repetition.

I'm not saying not to apply SOLID design principles from the start - I definitely try to separate concerns, giving a single responsibility to a class or function and avoid, for instance, a single method handling file reading, data transformation and reporting. This simplicity will make it much easier to refactor the code later.

I am saying to avoid using sophisticated design patterns, architecture or abstractions before you have used of your code enough to know what is likely to change (and require encapsulation) and what will stay constant (creating a useful abstraction).

My recipe for success

  1. Write simple code, leaning on the S from SOLID - separating code into small logical blocks
  2. When you find yourself repeating the same code 3 times, refactor it into a function or class
  3. Yes 3. When you have 3 sufficiently different examples of the same pattern, reach for design patterns and modularisation. Have fun. Don't be afraid to delete your existing code and start again using all you have learned.

Further reading

Conlin Durbin makes a similar point for front-end code in his article Stop trying to be so DRY, instead Write Everything Twice (WET).