Translation: Chen Philosophy

Welcome to visit NetEase Cloud Community to learn more about NetEase’s technical product operation experience.


You have an urgent task. You need to fix a bug. You need to get the product online right away.

But think about the future: every bug you describe will take even more time to fix later, and you shouldn’t be using deprecated APIs, outdated dependency libraries, and ways of working.

So when should you clean up your code?

Now?

Later?

Never?

In this article, I’ll review a set of heuristic strategies that can help you decide when to adopt these three solutions.

1. Update outdated dependency libraries and stop using deprecated APIs

2. Refactor to solve bad abstractions in the project

3. Organize in many ways — from breaking programming conventions to bad programming styles

Situational heuristic strategies

The prototype design

When you’re ready to tackle something with enthusiasm, you might start with a prototype (extreme programming calls it a spike). You don’t keep the code, you just explore problems and workarounds to see what you can learn from it.

Now that you’re throwing away the code, it doesn’t make much sense to update it and clean it up in many ways. Likewise, if you only need to understand existing apis or technical manuals, you don’t need to do a lot of refactoring.

On the other hand, if the goal of your prototyping is to find the right abstraction, you need to do a lot of refactoring.

1. Update: Never

2. Refactoring: designing new APIS or abstractions now; Otherwise, never

3. Organize in many ways: Never


The new project

When starting a brand new project, the decisions you make have a huge impact on the future maintainability of your code. This is the best time to apply the latest (most feasible) dependency libraries, project-related best practices, maintainable code, and the best abstractions you can think of. You probably won’t make all of these perfectly appropriate, but it’s well worth taking the time to try them out one by one and figure out what works as well as possible.

1. Update: Now

2. Refactor: Now

3. Now

Emergency error correction

In the face of users, you need to fix bugs as quickly as possible. Along the way, you may see a number of inadequacies in your code that are best addressed later if they are not closely related to the current bug.

Sometimes this means a second fix: a quick first and a second after necessary tidying up has been done.

1. Update: Later

2. Refactor: Later

1. 3. Smart refrigerator

New features or non-urgent bug fixes

When you have a project under development and you are doing continuous work, whether it be features or bug fixes, you have a good opportunity to clean up the code step by step.

You don’t need to fix everything every time you touch code. Step by step, clean up the current code you encounter, and over time, your code base will become maintainable. See on Jefferies’ Excellent Article for details.

1. Update: For currently encountered code, now

2. Refactoring: For the code currently encountered, now

3. Multifaceted cleanup: For the code currently encountered, now

A project in the maintenance phase

Eventually your project will be complete: not a lot of new development is done, and if parts of the program break down or new requirements come in, it will, for the most part, only need to be tweaked every few months.

Your job at this point is to get the project running with minimal effort. Refactoring, multifaceted cleaning, doesn’t matter, but updating does — dependent libraries may stop running or require security updates. Updating a dependency library five years in advance is usually much harder than updating a dependency library five times a year.

Therefore, whenever you need to fix a bug, you should update the dependent library — preferably with a long-supported version to reduce the need for updates to the API in use.

1. Update: For now, it’s best to have a long-supported version

2. Refactor: Never

3. Organize in many ways: Never

Weigh the present and the future

Software projects tend to be a cumulative process rather than an overnight success. Tidying up now may save you some time — but if you have an urgent task right now, it’s best to put it off, even if it takes more work later.

Therefore, this article is just a starting point: as with any heuristic, there are exceptions, and you need to make your own analysis based on your situation and goals.

Original: https://codewithoutrules.com/2018/11/02/when-clean-up-your-code/


Free access to verification code, content security, SMS, live on demand experience package and cloud server packages

For more information about NetEase’s technology, products and operating experience, please click here.


【 recommended 】 ShimmerTextView