Refactor - UnitTest - Trialm design in outdated code

How do you solve this problem when dealing with legacy code.

  • The classes you deal with are not well designed and require major design changes.
  • The classes you work with are mostly closely related.
  • You do not have enough test modules for a lot of refactoring.
  • You are not adding new unit tests because the design is bad and you will change it anyway.
  • You cannot easily change the design because
    • Tight connection, not enough modular verification - the material may be wrong, because it requires a new design for several classes at the same time without any security systems.

Where do you start? How do you attack a problem?

+4
source share
2 answers

The problem with the chicken and the egg.

If it is not possible to write some decent unit tests due to strong coupling, the best approach may be to work from top to bottom .

If you don't already have Integration, System, and GUI tests, this will be a good reason to create them before you start creating unit tests. Once you have them in place, you can start refactoring the code to create decent unit tests and still be confident that your comprehensive tests will understand your most obvious mistakes.

Please note that, in my personal opinion, these test files should be created in such a way that they should not be changed after you are ready to start creating unit tests and refactoring the code.

A must read on this issue. Effectively work with obsolete Michael Persian code .

Conclusion

The strategy outlined by Ive works for a wide variety of changes, but there are some reservations. Sometimes the only decent inflection point you can find for a set of classes is the boundary of the system. In some applications, the system boundary can be quite wide: it includes GUIs, calls to other external libraries, a database, etc. In such cases, the best way to get the invariant is to start by writing what Steve McConnell calls "smoke tests" against the rest of the system

+3
source

First, you want to make sure that you are not changing the behavior of the code. If you write unit tests that claim how the code currently works, you can make changes to the code and verify that the tests still pass, if they do, the code behavior has not changed. Then you can reorganize the code as you like. When you reorganize a design, you also reorganize the tests, but do not change the statements they make.

0
source

Source: https://habr.com/ru/post/1383310/


All Articles