Primitive Obsession
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
徵兆與症狀
- 使用基本型態而非類別來處理任務(例如:貨幣、區間、電話號碼…等等)。
- 使用常數來標示資訊。
- 在陣列中使用字串來代表欄位名稱。
出現原因
跟其他壞味道一樣,Primitive Obsession
一開始的時候也是不容易注意到。畢竟,開發者心理可能會想著:這只是個存放變數的欄位而已,使用欄位來處理,比起建立一個新類別簡單許多。漸漸的,其他欄位也用同樣手法處理。
處理方法
- 如果你有多個基本型別的欄位,或許可以嘗試將邏輯相近的欄位組合成自己的類別,可以嘗試
Replace Primitive with Object
。
- 如果這些欄位是拿來控制行為,可以考慮
Replace Type Code with Subclasses
,接著可以再以 Replace Conditional with Polymorphism
處理。
- 常常一起出現且四處傳遞的參數,可以用
Extract Class
及 Introduce Parameter Object
來抽取。