- Refactoring TypeScript
- James Hickey
- 364字
- 2021-06-11 13:05:50
When Should I Refactor?
It's already been mentioned that refactoring is best done in smaller chunks rather than in big sweeping changes.
Again, this avoids riskier and error-prone changes. You can also ship your product to your customers in a more iterative way and get feedback from your users earlier.
Then, the question arises: When should I refactor?
The Boy Scout Rule
There's a programming principle called The Boy Scout Rule (http://www.informit.com/articles/article.aspx?p=1235624&seqNum=6) which states:
"Leave the code cleaner than you found it."
This applies whether you are fixing a bug, adding a new feature, and so on. Just do something that improves the code you are working with.
Note
It could be as simple as adjusting the name of an ambiguous variable name.
It could be reorganizing the structure of your code's files.
It could be introducing design patterns to solve specific issues.
Repetitive Work
Ever run into a situation where you need to write code that you've already written before? Maybe the same code already exists somewhere else in the system?
If you find that you've had to write the same (exact) code over and over, then you might want to consider consolidating that code into a shareable resource.
That way, you don't have to change 12 different files to fix a bug or add a new piece of logic.
Difficulty Adding Features
As we mentioned previously, sometimes, you are faced with the task of adding new features to your code. However, perhaps the existing code is making it really difficult to just add that new feature or behavior.
In these cases, refactoring can help you mold the existing code into a place where it is easy to add new behaviors!
Note
This also applies to architectural issues. However, this book will focus on more code-based issues.
In the End
In the end, refactoring should be a continuous and habitual activity that you have chosen to engage in.
Martin Fowler, on the topic of refactoring (https://www.refactoring.com/), said:
"Refactoring isn't a special task that would show up in a project plan. Done well, it's a regular part of programming activity."
You shouldn't have to ask to refactor. Just do it when it makes sense.
- iOS Game Programming Cookbook
- Python科學計算(第2版)
- Objective-C應用開發全程實錄
- Three.js開發指南:基于WebGL和HTML5在網頁上渲染3D圖形和動畫(原書第3版)
- HBase從入門到實戰
- Java編程指南:基礎知識、類庫應用及案例設計
- Scratch 3.0少兒編程與邏輯思維訓練
- Python漫游數學王國:高等數學、線性代數、數理統計及運籌學
- Hands-On Swift 5 Microservices Development
- 多媒體技術及應用
- Clojure Web Development Essentials
- 少兒編程輕松學(全2冊)
- Python程序設計教程
- jQuery Essentials
- Java EE應用開發及實訓