Search code examples
phppythondynamic-languages

How to Make sure the code is still working after refactoring ( Dynamic language)


How to make sure that code is still working after refactoring ( i.e, after variable name change)?

In static language, if a class is renamed but other referring class is not, then I will get a compilation error.

But in dynamic language there is no such safety net, and your code can break during refactoring if you are not careful enough. You can use unit test, but when you are using mocks it's pretty hard to know the name changes and as a consequence, it may not help.

How to solve this problem?


Solution

  • Before you start refactoring you should create tests that will be able to test what you're going to change - if you say unit tests will not be enought, or they will be hard to create, then by all means create higher level tests possibly even excersising the whole of your product.

    If you have code coverage tools for your language use them to measure the quality of the tests that you've created - after it's reached a reasonably high value and if the tests are kept up to date and extended you'll be able to do anything with your code very efficiently and be rather sure things are not going in the wrong direction.