Slides with notes for the talk "Production code without tests" based on the Michael Feathers "Working Effectively with Legacy Code" book.
http://sstude.com/blog/2013/02/05/my-talk-production-code-without-tests
2. 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
3. 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
4. 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
5. #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
6. #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
7. #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
8. #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
9. 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
10. What is in the result?
• HDD - Hope Driven Development?
• FDD - Faith Driven Development?
• JDD - Jesus Driven development?
• PDD - Pray Driven Development?
• 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