惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
MyScale Blog
MyScale Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
MongoDB | Blog
MongoDB | Blog
I
InfoQ
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security

GeekPlux

一代人的博客,一代人的青春注脚 那些年我打过的日结工 来美国的两年后 2023 一蓑烟雨 在美国拥有一辆 Tesla 的成本 我的 Workspaces 十年进化史 How to Sync Logseq Plugins, Themes and Settings Across Multiple Devices Setting Up Umami as Your Google Analytics Alternative: A Step-by-Step Guide 迁移豆瓣读书记录到 goodreads Enhance Your Internet Privacy in 2023 Refactor your blog comments system with Webmention.io 来美国之后,如何快速安顿下来 Three Levels of Information Perception 疫情三年 与人聊天的美好 我获取信息的方法 2022 版 我是如何学会编程的 2020 恍如隔世 接外包的一些坑和小技巧 论交友 往返香港隔离指南 即将失明,还能继续做程序员吗 小谈我对新技术的态度 How to use tailwindcss with AMP in a Next.js project 远程工作如何提高效率 复式记账、财报、量化与图论 我为什么从阿里巴巴离职 2019 柳暗花明 投资被动型指数基金正在造成下一次金融泡沫? 三种主流的网赚套利,躺着赚钱?
Legacy code best practice: how to take over an existing p...
GeekPlux · 2021-01-27 · via GeekPlux

Legacy code best practice: how to take over an existing project smoothly

Every programmer, I guess, would meet the code from others that you have never contributed to. And one of the ways to distinguish experienced programmers from beginners is to see if they can quickly dive into the legacy code and maintain, develop, and refactor it in the right way.

There is no silver bullet to approach it, I’ve also been caught in the nightmare of not being able to read the code at all. But I have some tips from my experience to guide you to master it step by step. Hope it would be helpful.

1. Ask for documentation or explanation

It’s very lucky if there is some detailed documentation, or if the person who wrote the code is still around. This can save you a lot of time, but of course the best way is that the previous team can help you walk through the whole codebase.

In this stage, you’d better know:

  • how to run the whole project code
  • any easier way to test or debug during development
  • the version control tool they used
  • how to release and deploy
  • any dependent library you should notice

if there are no docs and no person was responsible for it available, promise me, please do not get angry and smash your keyboard :)

smash keyboard panda

2. Fix minor bugs

Learn by doing is always the fastest way to master code. For instance, if the legacy code is for the front-end, you can just change the Button style, if it’s for the back-end, you can just replace the response, that’s the easy but effective method to:

  • know and practice how to debug during developing
  • know each function’s purpose and its scope, roughly

These small bugs or tweaks allow you to get better familiar with the code, especially if you are really confused and have no idea how to start.

3. Keep new code clean

If you do not want to make things more complicated, then keep your new code clean. To follow the Unix philosophy, make each function do one thing and do it well.

You can’t control the quality of the inherited code, but you can make sure the code you add is the style you love.

4. Only rewrite code when necessary

I know every developer can’t help trying to rewrite the legacy code, but it usually brings much undesirable behavior.

Every time when I have to rewrite some functions, I will imagine the code taken over is a black box full of magic (and it always is!) so that I will be careful with each input and output.

When it comes to interdependent functions, try to control the scope of their impact, change the code as little as possible, try to ensure that no new bugs are introduced, and keep smooth iterative development.

5. Modularize code to the minimum possible

Maintaining and developing the legacy code is a progressive process. When you have to rewrite it, don’t rush to completely overturn the previous code first, try to take it apart first.

Dividing code makes you understand the code logic totally. You could keep the input params and output as simple as possible. Roughly split first, and then split again on top of that until it is minimized.

The advantage of this step is that any future refactoring of your code will also bring minimal impact.

modularize like lego

6. Refactor after modularization

Best practices for refactoring are written in many books, but I still recommend doing it on top of completing the previous step. Which will take weeks, months or possibly years depending on the complexity.

At the same time you need to keep up with the speed of developing new features, so it’s a trade-off. I know that refactoring parts of the legacy code can improve the speed of writing new code, but it can also be more of a burden. It needs much patience to take your time and get the refactoring done.

Conclusion

In short, don’t rush to overturn all the old code, but change it little by little, modularize and optimize it. Which like eating a pie, starting from the edges and working your way to the core, little by little.