How to make sure code still works after refactoring (dynamic language)

How to make sure that the code still works after refactoring (i.e. after changing the variable name)?

In a static language, if a class is renamed, but there is no other reference class, then I will get a compilation error.

But in a dynamic language there is no such security system, and your code may break during refactoring if you are not careful enough . You can use unit test, but when you use mocks, it is quite difficult to know the name changes, and as a result, this may not help.

How to solve this problem?

+7
python php dynamic-languages
source share
4 answers

Before you start refactoring, you must create tests that can test what you are going to change - if you say that unit tests will not be sufficient or it will be difficult to create, then by all means, create more high level tests may even ekstratiruyte your entire product.

If you have tools to cover the code for your language, use them to measure the quality of the tests you created - after it reaches a high enough value, and if the tests are updated and expanded, you can do anything with your code very effectively and make sure things don't go in the wrong direction.

+17
source share

I taught the class on unit tests, refactoring, etc., and this is probably what most people make mistakes. Refactoring is not just changing code. It changes the code without changing the external functional behavior. This is a very important point.

In other words, you need to somehow check whether the external functional behavior has been violated after refactoring. Without divine insight, I believe unit tests are very useful for this. In his book Refactoring, Martin Fowler emphasizes the use of automated tests for this test.

If your code was developed using TDD, you will have the necessary set of tests, as it is developed during the development of the code itself. If you need a refactoring code for which no tests exist, the best approach would be to set up automatic tests before you make any changes to the code. I understand that setting up tests for existing code can be difficult, but you will learn a lot about the code.

You can also check out Bruce Ekel's essay on strong typing and strong testing as it discusses the feedback you get from the compiler, the feedback you get from your test suite.

+10
source share

Your code may break during refactoring even in a compiled language. Based on this, you will be in trouble. Automatic testing is the best way to make sure that the program works as it should.

If you say what dynamic language you use, we can offer some tips on tools that will help you in testing. Everything can be tested.

EDIT:

You answered and said you were using PHP and Python.

If it is a web application, use selenium to create tests in the browser. First you just need a Selenium IDE. Put all of your tests in one test suite so you can easily complete all of them. As the list grows, you can begin your search for Selenium RC and selenium mesh.

+1
source share

1) For Python, use PyUnit for PHP phpunit. 2) The TDD approach is good, but it also makes tests after writing code acceptable. 3) Also use the refactoring tools that are available for your IDE, they only do safe refactoring. In Python, you have a rope (this is a library, but there are plugins for most IDEs). 4) Good books: "Test development using the example of" Best "Expert Python Programming" Tarek Ziade (explain both TDD and refactoring)

google tdd and the database to find a good book on the TDD approach for database development.

Add information for the midges you use. AFAIK fantasies are only needed when using a database or network. But usually unit test should cover a small part of the code (only one class), sometimes two classes, so the layout is not needed !!

0
source share

All Articles