Working Effectively with Legacy Code: Chapter 8 Summary

In legacy code, one of the most important considerations is that we don't have tests around much of our code.

What's worse, is that getting them in place is difficult.

That's why many people are tempted to go back to the techniques we learned in the last chapter (sprouting and wrapping).

There are three main problems with this approach:

  • We don't significantly modify the existing code, so it isn't going to get any better.
  • This approach may encourage duplication which just lies there and festers.
  • Because we are avoiding the root problem we encounter fear and resignation. Fear that we can't change a particular piece of code and make it easier to work with, and resignation because whole area of the code just aren't getting any better.

In general, it's better to face the beast than hide from it. One great way to do that is to write tests while your adding new features.

This is called Test-Driven Development.  


Hi, I'm summarizing the book "Working Effectively with Legacy Code". If you are interested in the book, feel free to check it out here.

Working Effectively with Legacy Code
Get more out of your legacy systems: more performance, functionality, reliability, and manageability Is your code easy to change? Can you get nearly instantaneous feedback when you do change it? … - Selection from Working Effectively with Legacy Code [Book]

Test-Driven Development (TDD)

Source: https://www.xenonstack.com/blog/test-driven-development

Test-Driven Development (TDD) is a feature-addition technique that before you write any code, you should write a test for it first.

The TDD Algorithm goes like this:

  1. Write a failing test case.
  2. Run all tests and see if any new test fails.
  3. Fix the failing test.
  4. Run tests and refactor code.
  5. Repeat

Benefits of Test-Driven Development

The following are the main advantages of TDD:

  • Regression Testing – Using TDD you build up, over time, a suite of automated tests that you and any other developer can rerun at will.
  • Better Design – If your dependencies are all tangled up then it's gonna be difficult to write tests. That's why when you write tests first, it forces your brain to better design your actual code.
  • Confidence – It makes refactoring much easier because you have a set of automated tests that you can fix before release.
  • Easier Onboaring –  Having a project with tests ready can be easily picked up over a project with no tests.
  • Saves Time in the Long Run – Though developers have to spend more time in writing TDD test cases, it takes a lot less time for debugging and developing new features. You will write cleaner, less complicated code.

Conclusion

To be honest, there isn't really much else to say.

Just write your tests first.

Thanks for reading.