如何找出Legacy Code中的Bad Smells

Smells Like Legacy Code
http://www.testearly.com/2006/12/14/smells-like-legacy-code/


如果你要知道你程式碼中的健康狀態, 你要如何檢查呢?

就像身體健康檢查一樣, 你必要知道你想檢查哪些問題(肝臟, 心臟或是過敏等等), 接著再看有哪些index可以呈現相關的狀態, 然後健檢人員會使用工具來做檢查, 最後你就會拿到一堆數據, 並且告訴你在哪個範圍內是好的, 還是有問題的. 如果真的是有問題, 醫生還會告訴你, 要做什麼事情會讓情況好轉.

對於程式碼也是一樣的, 你要先知道你想看的bad smells是什麼, 接著就看有怎樣的index, 可以表達目前的狀態如何. 然後你就用一些tools來parse source codes, 看看它的狀態如何. 如果不是很好, 這時候一些refactoring 或是design pattern, 便可以來應用看看.

作者列了三個samples讓大家看看, 並且告訴大家說他已經開始收集這些資料, 希望他功能圓滿的那天, 可以分享給我們知道.
1.
Smell: Conditional Complexity
Measure: Cyclomatic Complexity
Tools: IDE, CCMetrics, Source Monitor, JavaNCSS, Eclipse Metrics
Refactoring: Extract Method, Extract Class, Replace Conditional with Polymorphism
Pattern: Abstract Factory, Strategy

2.
Smell: Long Method
Measure: Number of Lines in Method
Tools: IDE, PMD, Eclipse Metrics, CheckStyle, Source Monitor, FxCop
Refactoring: Extract Method
Pattern: Abstract Strategy

3.
Smell:
Measure: Depth of Inheritance (DIT)
Tools: IDE, PMD, Source Monitor
Refactoring: Replace Inheritance with Delegation, Pull Up/Push Down Method
Pattern: Delegation


arrow
arrow
    全站熱搜

    kojenchieh 發表在 痞客邦 留言(0) 人氣()