Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 779
Graviton Avatar asked Mar 27 '09 06:03

Graviton


2 Answers

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.

like image 155
RnR Avatar answered Oct 23 '22 00:10

RnR


I've been teaching a class on unit tests, refactoring and so forth, and this is probably the thing that most people get wrong. Refactoring is not just changing the code. It is changing the code without changing the external functional behavior. That is a very important point.

In other words, you need to have some way to verify that the external functional behavior is intact after the refactoring. Lacking divine insight I find unit tests very useful for that. In his book on Refactoring, Martin Fowler stresses the use of automated tests for this verification.

If your code was developed using TDD you will have the necessary test suite as it is developed during the development of the code itself. If you need to refactor code for which no tests are available, your best approach would be to set up automated tests before you make any changes to the code. I realize that setting up tests for existing code can be hard, but you will learn a lot about the code while doing so.

You may also want to check Bruce Eckel's essay on strong typing versus strong testing as it discusses the feedback you get from the compiler versus the feedback you get from your test suite.

like image 23
Brian Rasmussen Avatar answered Oct 23 '22 02:10

Brian Rasmussen