1/06/2017

重構的路上

begin..
更新完 2 篇 2016的紀錄後(居然只有兩篇),2017一開始的挑戰就是對大型專案做架構分析整理 (ドキドキ)
之前陸陸續續有在接觸的 refactor 應該頗有幫助的吧,我想這應該可以成為銜接的第一步 XD
開始整理一些 Pearson 出版的 Refactoring: Improving The Design of Existing Code,重構雖然也可以靠經驗累積,但工具書的存在除了檢查以外,也有反思的幫助。 因為前面的概念比較不需要特別整理,筆記只想從第六章作起,最後完成到哪裡還是未知數,反正有必要的話也會對筆記重構一下(懶人發言)

Toward a Catalog of Refactoring

Format reference :
- name
- summary: what’s the problem, what to do, quick picture(code/UML)
- motivation:如果提煉動作可以強化程式碼清晰度,那就去做。
- mechanics
- example

Extract Method

about extract…
是最常見最基本的手法 : 提煉整理
[按] 大量簡化過長的函式

Inline Method

about Inline…
只被用過一次就 inline it (Replcae Temp with Query )
可以利用 final 做檢查
[按] 避免寫出更長的函式
[按] 利於檢視更多重構的可行性

int getLevel() {
  return mAutoMode ? 0 : mCurrentLevel
}

Inline Temp

final the local var then inline it

Replace Temp with Query

ex: local -> final -> local temp => inline temp

Introduce Explaining Variable

利用 declare var 來介紹複雜的運算式
其實常常也可以用 extract method 就好了

Split Temporary Variable

abt split…
當 var 被賦予超過一次值,結果具有多重意義時 (neither loop var nor collecting temp var)
[按] 一個暫時變數只有一種目的

Remove Assignments to Parameters

abt remove value assignment..
避免混淆 pass by value and pass by reference
Java沒有pass by reference呀!!所以像在Java,多餘的賦值動作沒有意義.

Replace Method with Method Object

turn method to object..
當 local var 氾濫又無法簡單分解的時候,把需要整理的區塊以物件 (class object) 的方式重新詮釋。 同樣的,可以使用 final var first 的方式,再用 (class object).compute() 去做簡化處理。
[按] 讓冗長的程式更清晰在做什麼
[按] 似乎可以達到意料外的效果!

Substitute Algorithm

如同字面敘述,舉例是利用 List 簡化迴圈中的多個 if。
[按] 可試。

Moving Features Between Objects

To make sure if the class is ‘Responsible’

Move Method/Field

當某 函式/欄位 在另一class更廣泛的被引用時,是否因為責任歸屬不清?
* move field 的時候注意 Self Encapulate: Replace Local Var with Query
* Self Encapulate 可以確保不出錯地保持小步前進

Extract Class

就是一個精簡提煉 Class 的手法,需要思考的點在於是否讓新的 class 曝光:
reference object or immutable value object ?

Written with StackEdit.

沒有留言:

張貼留言