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

Production Code Without Tests v2

Production Code Without Tests v2

Second version of my talk about the legacy code without tests and the ways we can improve that

Akim Khalilov

May 31, 2013
Tweet

More Decks by Akim Khalilov

Other Decks in Programming

Transcript

  1. About me: Developing e-commerce applications for Eastern Europe: • 8

    countries • 3 web-application • 1М+ users @sstude http://sstude.com
  2. Agenda o Why the code without tests is a problem?

    o Real-world example of our application. o What do we need to change – code, and not only? o Profit.
  3. Problem is that o It’s complicated • Hard to understand

    o A lot of “gotchas” • It’s hard not to break anything o It happens every time...
  4. Story of one application o 9 countries, 1k operations with

    orders per day o 6+ years of development o Absence of tests, documentation, business analysts (changing) o All responsibility is on developers (two DEVs)
  5. Story of one application o People is happy while app

    works o Request for a new feature o Problem – do not break the code • During development • During bug fixing • During refactoring
  6. Code is not ready for unit tests o Structure violation

    o Tight coupling o Spaghetti-code o God object o Static method calls (a lot) o Dependencies on external resources (services, file system, database, ...)
  7. o Several classes per one file o Flat solution structure

    o Duplicate structure o Absence of grouping by some principles (internal / external) Structure complicates the search
  8. Structure o Extract namespace and folders o Avoid duplication o

    Group by business rules (Processors, DeliveryHandlers, …) o Group by technology (NH, Forms, Controllers, … ) o Group by assemblies helps
  9. It is better, but We still are dealing with real

    service o Extract an interface for your code o Write a “wrapper” – for the third- party code the best, is to
  10. How to resolve the problem?  Do not use static

      Exclude call to static methods for unit tests: o Subclass and Override Method o Extract and Override Call
  11. It’s better, but if we need more? We need to

    override two methods Extract and Override Call
  12. “Proper” unit-test frameworks Do not use the ones, that just

    do intercept the call – there is the risk to leave the system as is, without any change
  13. New code in legacy New code could be covered by

    tests – it’s new  Implement it in a new method / class (Sprout method / class) Call it from the legacy code
  14. Small-methods problem o Temporary state of the design o Step

    to refactor the system o Step to write unit tests o With the idea to refactor the system globally
  15. This is not going to work in my team You

    need:  Arrange your code – refactoring  Arrange your team – idea  Arrange your management - time
  16. Arrange your team o New code - with test o

    Bug fix - with test o Code is not ready o Can take more time o Refactor your code continuously (time?) o Run your tests continuously - CI
  17. Arrange your team Pay attention to red tests. Not pure

    TDD: Did the broken test was fragile? Or the system was broken and the test did saved you…
  18. Arrange your team Time balance between refactoring and new tests.

    If you have no time, or there is complete overtime for your team – enthusiasm will end very soon.
  19. Arrange your management Time balance between refactoring and new tests.

    Integrate to your existing iterations (Refactoring in overtime would not work too long)
  20. Risks o Hard to do less (wish to change everything)

    o Tests will help in the future– it’s an investment o Management will fight back (time is money)
  21. Profit  An investment to the code  Test will

    save in the future  Improve the system during refactoring  System is resistant to changes  Business-logic is well-documented for the team and new people