Programmer, why don't you rewrite the code?

Source: Internet
Author: User

As a 100offer programmer auction operation, we often talk with users, there is a topic enduring: the programmer to take over the new company after the existing code, how to deal with it?

Programmers have the heart of an engineer, so the first thing they want to do when they go to a new site is to bring the old things back. Yes, they will never settle for simple incremental labor.

Perhaps this subtle psychological orientation can explain why programmers would rather throw away old code or tinker with the new project than they thought the old code was a mess.

But is it true? The reason you think the old code is a mess is actually determined by a basic law of programming: It's easy to write code and hard to read code.

Why do you think the old code is so messed up? Because reading code is more difficult

This is probably why code reuse is difficult to implement, but it can also explain why everyone in your group likes to use different functions to convert a segmented string into an array. It's easier and more fun to re-write a feature than to guess how the old features are implemented.

As a corollary of this axiom, you can ask the programmers around them what is the code they are fighting for? "What a mess!" "They are sure to say so." "I just want to knock it off again!" ”

Why do you think the code is so bad? "Well, look at this feature, there are two pages long!" I don't know why these things are here! I don't know what these APIs are all about. "They will answer you like this."

Comics: What is the experience of reading someone's code?

Borland's founder, Philippe Kahn, once boasted to reporters that Quattro pro would be much more useful than Microsoft Excel because it was written from scratch, all new source code!

However, it is absurd to think that the new code is better than the old code. The old code is already running and tested. Countless bugs were run online before they were discovered, and programmers may have spent a few days fixing the bugs. This fix could be a single line of code or a few characters, and countless hours and energies are spent on these bug fixes.

When you decide to abandon these old code from scratch, you also lose the results of all your predecessors ' efforts.

Must the new code be better than the old code? NO, rewriting may pose a greater risk

Rewriting the code for a project is also an unusually difficult decision for a technology leader. Because from the company level, reproducing the code can even threaten the market competitiveness of the product. Once you decide to rewrite the code, you may be 2-3 years behind the competition-in the software industry, which is long enough.

Your ideal new code will lead to improved product functionality



But in fact, even if the new code is rewritten to implement all the functions and requirements of the old code, there is only marginal improvement in the market competitiveness of the product. Because of the new technology, new language and new framework of rewriting, the product has not brought a qualitative leap.

Not to mention the lengthy process of rewriting may encounter some unexpected situations, such as:

1, lack of money: the break of the capital chain



2, the shortage: core programmer quit

The result is not effective: not to the original product should have all the functions and requirements, wasted time and money, but also lost the market competitiveness.


So rewriting the code means you're putting yourself in a very dangerous situation, and maybe you won't be able to write a better code in a few years. You just spent a lot of money writing the existing code again.

What do you do when you think the old code in front of you is rotten?

You think the old code is bad, so what? They are already online and have withstood the test in real-world operation. So when you find that the code left by your predecessor is messy, you might as well calm down and understand the code and improve the code in the following three ways:

1, the structure of the code has a problem

If a piece of network code suddenly pops up its own dialog box, it should be UI code that needs to be processed. These problems can be solved, you need to carefully move the code, refactoring, change the interface. A careful engineer is also required to immediately examine the changes carefully to see if there are any problems, so as not to disturb others. In fact, even larger structural changes can be done without throwing away the code.

Daniel Programmer Joel Spolsky recalled that he and his team had spent months in a project re-architecting at one point: moving code around, cleaning up, creating meaningful base classes, and creating the perfect interface between modules. But they were always very cautious and did not produce new bugs or discard any old code.

2, the efficiency of the code is not high

Once, Netscape's rendering code was transmitted very slowly. But in fact, this only affects a small part of the project, which you can optimize or even rewrite. You don't have to rewrite all the code at all. The 1% effort to optimize your speed will give you a 99% explosive boost.

3, the code is very ugly

Some of the code is really ugly, such as Joel, who was involved in a project, began using underscores to start a member variable convention, but later switched to a more standard "m_". So half of the functions start with "_" and half start with "M", which looks really ugly. But this problem can be solved in 5 minutes without having to write all the code from scratch.

Finally, keep in mind that writing from scratch does not mean you will write better code than before. Because you are not involved in the creation of the previous version, you are not really experienced at all. Once you're ready to push the rewrite, you'll probably make the same mistake again, and even create more new problems.

Summarize

In the face of bad old code, Keep Calm & Carry on!

In large commercial projects, it is very dangerous to reinvent the line. Of course, if you're experimenting, think of new algorithms that can be rewritten at any time. If you're on a job-hopping, or just taking over a new project and looking at the old code that looks like a mess, calm down and resist the urge to rewrite, think of the above experience.

Programmer, why don't you rewrite the code?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.