代碼重構 code refactoring
There are two general categories of benefits to the activity of refactoring.
- Maintainability. It is easier to fix bugs because the source code is easy to read and the intent of its author is easy to grasp.[4] This might be achieved by reducing large monolithic routines into a set of individually concise, well-named, single-purpose methods. It might be achieved by moving a method to a more appropriate class, or by removing misleading comments.
- Extensibility. It is easier to extend the capabilities of the application if it uses recognizable design patterns, and it provides some flexibility where none before may have existed.[1]
Purpose
- 在極限編程或其他敏捷方法學中,重構常常是軟體開發迴圈的一部分:開發者輪流增加新的測試和功能,並重構代碼來增進內部的清晰性和一致性。自動化的單元測試保證了重構不至於讓代碼停止工作。
- 重構既不修正錯誤,又不增加新的功能性。反而它是用於提高代碼的可讀性或者改變代碼內部結構與設計
- 移除死代碼,使其在將來更容易被維護
- 一個重構的小範例是修改一個變數的名稱使其具有更明確的含義,例如從單個字母的「i」重構為「interestRate」。較複雜的重構是把一段if區段中的代碼變為一個子程式。
來源
- 重構這個術語是從數字與多項式的因式分解類比而來。如,x2 − 1可以被分解為(x + 1)(x − 1),這樣揭示了前面的形式不可見的內部結構(如兩個根+1和−1)。同樣,在軟體重構中,在可見結構上的改變通常會揭示原代碼中「隱藏」的內部結構。
沒有留言:
張貼留言