l

2015年1月21日 星期三

更精確的壞味道定義(中)

Jan. 20 08:28~09:18

image

 

這一集繼續談〈Improving the Precision of Fowler’s Definitions of Bad Smells〉這篇論文對於Middle Man和Speculative Generality壞味道的定義。

***

Middle Man(中間人)

Fowler書中對於Middle Man壞味道的定義如下:

You look at a class’s interface and find half the methods are delegating to this other class.

論文對於Middle Man壞味道的定義以下列兩點表示(直接引用論文的內容):

  1. Half of a class’s methods are delegation methods.
  2. A delegation method is a method that.
    • Contains at least one reference to another Class.
    • Contains less than a threshold value of LOC.

論文特別針對delegation method的定義加以說明,一般大家對於delegation method的認知是它會把request直接轉呼叫其他method。但是如果一個有50行的method,其中只有若干行,例如5行是呼叫其他method,這個50行的method還可以算是delegation method嗎?所以論文把delegation method的行數定了一個門檻值,要小於這個數值才算。

關於Middle Man壞味道,可以參考〈談談壞味道(9):Middle Man〉。

***

Speculative Generality(夸夸其談未來性)

Fowler書中對於Speculative Generality壞味道的原始定義為:

If the machinery was being used, it would be worth it. But if it isn’t, it isn’t. The machinery just gets in the way, so get rid of it. This kind of machinery includes: abstract classes that aren’t doing much, methods with unused parameters, methods named with odd abstract names.

這篇論文把Speculative Generality壞味道以下列兩種狀況來表示(直接引用論文的內容):

  • 狀況1
    1. A class is an abstract class or interface.
    2. This class has not been inherited or is only inherited by one class.
  • 狀況2
    1. A class contains at least one method which contains at least one parameter which is unused.

Speculative Generality是一個比較抽象因此不容易光從程式結構判別出來的壞味道。Fowler在書中提到三種情況:

  • 沒幹什麼事的抽象類別
  • 函數(method)有沒被使用到的參數
  • 函數的名稱包含多餘的抽象涵義

論文中只提到前兩點,第三點是語意的問題,程式很難自動判斷。

關於Speculative Generality壞味道,可以參考〈談談壞味道(7):Lazy Class & Speculative Generality〉。

***

友藏內心獨白:牽扯到語意就很難處理。

沒有留言:

張貼留言