「冗余類別(Lazy Class)」也被稱為「遊手好閒者(Freeloader)」。當你在專案內發現存在一個無所事事的類別,幾乎沒有實作任何方法與職責,正是屬於這種程式碼氣味。
也許這個類別最初完成時是被設計成具有完整功能的,具備所有必要的方法和實作能力。然而,在經歷多次重構和優化迭代以後,它不知不覺之間變得非常小巧輕薄、甚至可有可無。也可能是這個類別最初為了支援未來的某個專案而設計,但這些計劃工作從來沒有被正式實施或完成。這可能是出於各種原因,例如優先事項的改變、資源不足或使用者需求的變動等。儘管如此,這個類別仍然在專案中保持最初的狀態,持續等待著時機來臨好去實現其最初的設計目的,並期待能為未來某天的開發工作做出貢獻。然而,多數的類別無法等到期望的那天到來,就這樣持續在專案的角落中保持原狀。
順帶一提,冗余類別是「大類別(Large Class)」的相反氣味,分別是類別太小與太大。
以下是為何冗余類別會被視為程式碼氣味的一些原因:
如果在子類別中發現冗余類別氣味,我們可以考慮將其所剩不多的功能與實作通通向上合併到父類別之內。
如果眼前這個類別的功能有限,沒有具體的職責,也沒有被分配的任務,未來也沒有計劃要賦予它額外的功能。在這樣的情況下,我們可以將這個類別的所有功能全部轉移到另一個類別中。
單例(Singleton)是一種設計模式,確保一個類別同時只存在一個實例(類別實體),並提供對該實體的全域訪問接口。如果發現實作單例的類別具有冗余氣味,我們可以考慮將其轉換為「內聯單例」。
首先,仔細審查冗余類別中單一實體的方法和屬性,以了解其功能以及它如何被使用的。接下來,從冗余類別中複製方法和屬性,並將它們直接複製貼上到使用它們的類別中。
一旦我們完成修改了使用這些單例方法和屬性的類別,我們現在就可以安全地刪除原始的冗余類別。
Lazy Class also knows as Freeloader. When you encounter a class that isn’t doing enough to earn your attention, it’s a sign of this smell.
Perhaps a class was initially designed to be fully functional, with all the necessary features and capabilities. However, after undergoing multiple rounds of refactoring and optimization, it has inadvertently become ridiculously small in size.
Alternatively, the class may have been initially designed with the intention of supporting future development work that was never implemented or completed. This could be due to various reasons such as shifting priorities, resource constraints, or changes in project requirements. Regardless, the class remains in its current state, waiting for the opportunity to fulfill its original purpose and contribute to future development efforts.
Lazy Class is the opposite code smell to Large Class.
Here are some reasons why Lazy Classes are considered a code smell:
If you find the Lazy Class smell in a subclass, we can consider merging it into the superclass.
The class currently has limited functionality and no specific responsibilities. It does not have any assigned tasks, and there are no plans to give it any additional responsibilities. Therefore, it is suggested to transfer all the features from this class to another class.
A Singleton is a design pattern that ensures a class has only one instance and provides a global point of access to that instance. If you encounter a class with a code smell of Lazy Class and implement it as a singleton, you may consider transforming it into an inline singleton.
First, carefully review the methods and attributes of the Singleton instance in the Lazy Class to understand its functionality and how it is used by the consuming class. Next, copy the methods and attributes from the Singleton instance in the Lazy Class and paste them directly into the consuming class.
Once you have modified the consuming class to use the copied methods and attributes, you can safely delete the original Lazy Class.
https://refactoring.guru/smells/lazy-class
https://www.informit.com/articles/article.aspx?p=1400866&seqNum=12
https://refactoring.guru/design-patterns/singleton