Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Production code without tests

Production code without tests

This talk is about the legacy code and the way we can refactor it. Details in my post
http://sstude.com/blog/2013/02/05/my-talk-production-code-without-tests/

Akim Khalilov

November 28, 2012
Tweet

More Decks by Akim Khalilov

Other Decks in Programming

Transcript

  1. Any problems in the code? • The code works –

    everybody is happy  • And here you are asked to implement the new feature • Problem – DO NOT BREAK THE CODE – During development – During bug fixing – During refactoring Akim Khalilov Production code without tests 2
  2. Change the things • Cover all your cnages by tests

    • Who is asked, «Can we write tests to the existing app?» • You remember the answer  Akim Khalilov Production code without tests 3
  3. Code is not ready for unit tests • Structure may

    be violated • Long methods / classes – God object • Static calls • Dependency from the external resources Akim Khalilov Production code without tests 4
  4. #1: Objects instantiated explicitly var service = new WebService(); Solution:

    • Constructors (injection) • Method calls (injection) • Interfaces (for loose coupling) • Do not have interfaces - wrappers • DEMO #1 Akim Khalilov Production code without tests 5
  5. #2: Static call WebService.CalculateBy(index, range); Solution: • Do not use

    static  • Exclude static call in the test • Extract And Override Call • Subclass And Override • DEMO #2 – The Seam Model Akim Khalilov Production code without tests 6
  6. #3: New feature in the legacy code Solution: • Do

    not touch the legacy code • Call new code from the legacy one • Sprout Method • DEMO #3 Akim Khalilov Production code without tests 7
  7. #4: New feature without pain The problem: • New logic

    is breaking call signature Solution • Default implementation • Wrap Method • DEMO #4 Akim Khalilov Production code without tests 8
  8. I need make changes, but I don’t know what kind

    of tests should I write • Characterization test: – Documents current code behavior – Exclude any «I think, «Maybe the code works this way..» • Write the red test. Error will show you what returns from the method • If the result is illogic – take to the look (Bug?) • Every new test: – Documents the function: What does that method do? – RTFM Test – Helps make changes without fear Akim Khalilov Production code without tests 9
  9. What is in the result? • Investment to the code

    • Tests will save your back in the future • Team knows code better • The system is under refactoring Akim Khalilov Production code without tests 10 • HDD - Hope Driven Development? • FDD - Faith Driven Development? • JDD - Jesus Driven development? • PDD - Pray Driven Development?