SOLID Object Oriented Design by Sandi Metz

Youtube Source

  • Design is all about dependencies
    • If you refer to something, you depend on it.
    • When the things you depend on change, you must change.

Problems

Source Code

  • About the code
    • What if the ftp host/login/password changes?
    • What if I need to create another job like this?
  • About the test
    • What if I don’t want to ftp a file in every tests

Notes

  • If testing seems hard - examine your design.
  • TDD will punish you if you don’t understand design.
  • Resistance is a resource Listen to what the pain is telling you. Listen to what the code smells are telling you. Embrace the friction. Fix the problem.

RED - GREEN - REFACTOR

Notes

  1. Is it DRY?
  2. Does it have one responsibility?
  3. Does everything in it change at the same rate?
  4. Does it depend on things that change less often than it does?

Ask this questions when you are in GREEN phase. If the answer is YES You should change your code.

”Triangle of Responsibility” Refactoring

Notes

Don’t guess! Follow the rule and see what happen!

  • Refactor, not because you know the abstraction, but because you want to find it.

  • When you see someone’s code and think it’s beautiful and you wonder how they thought of it, they didn’t. They evolved it to that point.

  • How to assess: “Does each object depend on things that change less than it does?”

    • After refactor: The order of dependencies: PatentJob => FtpDownloader => Config => patent.yml
    • Line up the objects from left to right
      • Left = lower likelihood of change
      • Right = higher likelihood of change
    • Only depend on things on your left

= Less ============= Likelihood of Change ================= More ===

Always depend on things on your left

The Bottom Line

TDD is not enough. DRY is not enough.

Design because you expect you application to succeed and the future to come.

References