Refactor the code, you really ready

I believe that every project had received the old programmers may have Tucao the "previous code is feces." A project for some years, almost certainly can see - everywhere copy to copy to code, you can see the misspellings, top-heavy functions ...... then take a look at the year submitter may be a veteran of the company, even big boss, my heart can not help but secretly despise, wondering if he himself entered into the wrong company.

And you are assigned to take over the general code that lump "feces" and to add more features on it, every time you make additions and deletions code perpetuity, each encounter a bug in the original code will make your hair line up again.

Finally one day, you could not help, their minds are full of an idea - I want to rewrite the code. Then you really do spend a whole night / days / weeks, put the code into a satisfied look of your heart. Then the line code:

Happy Ending: reconstruction code to get blown away by colleagues, we have to praise the code you write much better than before.

Normal Ending: After a few months, you find the code refactoring and die, plus a new feature costs Si Jin, so once you turn in the reconstruction planning.

Bad Ending: After the code is on the line reconstruction, bug constantly, let your boss call serial killer overnight repair, you find the old code is written so not without reason.

Such stories each had handled the old project programmers may have a somewhat similar experience in my career has gone through several reconstruction handful, but every reconstruction experience almost stepped on each every kind of pit.

Do you really need to reconstruct it

Before reconstruction project, be sure to repeatedly ask yourself (and your crew) this question: Do we really need to reconstruct it?

Reconstruction project, under the premise of just reconstruction, for the company's earnings is --0, because the user of your product, they will not come and buy it for your remodeling behavior, for them, your source code written in a good-looking or not does not matter to them important is the product itself has not improved. For companies, remodeling behavior not only did not bring any benefit, but consumes resources programmer, the loss for the company.

An Internet product life cycle may be only a few short years, put a little bit longer see, now write the code may be too useless a few years will, in this context, the reconstruction of existing projects, it must be established on the basis of the project itself is also very promising, based on how much potential this project, it is worthwhile to reconstruct the future? If the product itself does not do much, then Is it worth taking the time to reconstruct it?

Why the need for reconstruction projects

Reconstruction of the reasons for each project varies, but mostly personal summary to the following two points

  1. Too many loopholes in the original project, or poor stability, the current framework is difficult to completely cure.
  2. The new project requirements, the original program framework has been unable to meet.

Assuming your project is not a lot of bug, stability is also very good, no new or temporary demand under the existing framework is difficult to achieve, it is not recommended for reconstruction projects.

I was in the company of the SEM group work experience first reconstruction, it is the result of the auction background bid calculated by the database tables (Table) stored pushed into a queue system (RabbitMQ). Background bid bidding process to calculate the results need to be uploaded by another program to upload Adwords and other auction platform, we are in the last practice is established in a database table in which the program is regularly uploaded bidding program will calculate the new storage auction to query records in the table newly added, upload them in bulk, and delete after uploading. So why should this be reconstructed?

  1. With the increase of the company's ad serving, single instance upload process is difficult to upload all of your bids in a short time, but if you run multiple instances of the upload process, a number of examples occurs while new inquiries into auctions and upload, delete the same record caused by a database deadlock. ( 1 original project too many loopholes, or poor stability, the current framework is difficult to completely cure. )
  2. New business requirements need to calculate another auction format, if you continue to use the database table to store, you need to either an existing table field expansion / modify, or create a new form. But at that time it could have foreseen the auction format support more in the future, so the storage database table will no longer flexible. ( 2. The new project requirements, the original program framework has been unable to meet. )

Reconstruction project

After repeated measure, we finally decided to reconstruct the project, congratulations, you will have some step on pit tour.

Before reconstruction project

The first step in the reconstruction project is to understand the project.

Reconstruction of a class of error most likely to occur is not able to complete the original function faithfully reproduce it. Author of many project developers are not at hand, and the project has also been a long time iteration, when the code snowball when almost no one (including the original author and product managers) to fully understand the project in the end included what. When the original function exactly the same after you see the reconstruction and testers nor measured when something goes wrong, maybe one of these years which add special features horse coming months, it is quietly kill you. Wait on the line, this special function the user suddenly found not function, so come complaints.

Reconstruction of ING - test

If you say what is the most important first step in the reconstruction, I think it is a test.

If the original code is not unit testing, integration testing, able to do so be sure to add on. Why the test is so important? Than playing a good, as if in front of a reconstructed old key with a new key to, while the test code is the key to the old mold, we make a new key to be able to come out of this mold and for the whole. The more detailed the mold, the greater the probability of a new key can unlock the normal.

I recall a major mistake appeared in past reconstruction, is in the reconstruction process, the original there is a unit test error has occurred, the original assertion that the result is NULL, but my result is 0, then that might are two results, then the result of the wrong choice unit testing "correct" result in the code on the line, a result quite different compared to 0 causes the program output before and. To summarize: 1. If there is integration testing, such an error can be found on line before. 2. It is believed that the original set of unit tests should not be "taken for granted" in the reconstruction of their own to think logically correct.

Reconstruction of ING - branch

Procedure code refactoring, we must not recommended to remove the code completely rewritten. More recommended that the first copy of a new function / file / folder, and then write new code. Why should I do?

  1. When writing new code can While he wrote, referring to the original code.
  2. Code review of the new code (Code Review) will be relatively clean.
  3. History project management tools (Git, SVN) is relatively clean.

Back to what I said above, the database table (Table) is stored into a reconstructed pushed to the queue system (RabbitMQ), when my practice is that the auction program end, to re-implement the function of the output, making the auction results pushed to the queue system. In the upload process ends, then re-implement a new program, only the consumer from the message queue pushed message, and then upload to Adwords and other advertising platforms. Old upload the original program is not the slightest change.

Reconstruction of the line item - Switch

Slightly larger reconstruction, I would rather recommend using the program switch, using some control logic parameters to control the entrance is the old code or new code, there is a problem on this line, can timely adjustments to control parameters, quickly roll back to old logic.

If the result of running the program itself is uncertain, is not easy to see the reconstruction of errors and even recommended setting A / B test in the reconstruction of the entrance, so that part of the flow logic reconstructed go first on this line, while the new / old logic flow mark into different test bucket, you can see how the performance of the new and old code on the data measurement platform. If the performance of the new code is reasonable, it can cover the increasing flow of new code, up to 100%.

In the reconstructed I mentioned above, the program calculates the auction I choose to create a new segment of A / B test, the control method writes the results to a database of bid, bid experimental results will be sent to the message queue. At the same time in a production environment, the old and the new upload programs running at the same time. When just on the line, I chose the one percent of the auction results pushed the message queue, and then observe whether the new upload program message queue of messages consumed. Meanwhile, in the monitoring of the product page on the auction results in the control group and the experimental group were analyzed to confirm the auction results of the two groups were not significantly different.

to sum up

Summarize the personal experience of reconstruction, if necessary, before reconstruction, reconstruction good test, branch switch.

Guess you like

Origin www.cnblogs.com/chaosyang/p/are-you-ready-for-refactoring.html