Last Revision — April 19, 2022
1 Min Read
---
Object Oriented Abusers
Data
Within a Class
- Binary Operator in Name (co-exist)
- Long Parameter List (causes)
- Large Class (causes)
- Special Case (causes)
Martin Fowler in book (1999): "Refactoring: Improving the Design of Existing Code"
Temporary Field is a variable created where it is not needed. It refers to variables only used in some situations [1] or specific areas of a program. This uniqueness can be confusing when the purpose of using the variable cannot be explained or cannot be found outside of its scope [2]. It might be misplaced at the class level when the functionality it provides is specific only to a particular method [2]. One should expect an object to need all of its fields.
Additional fields clutter the code and strain the cognitive load by keeping in mind useless attributes.
Consider the following example, when full_date
is used only for an object to string conversion.
@dataclass
class MyDateTime:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
self.full_date = f"{year}, {month}, {day}"
def foo(self):
...
def goo(self):
...
def hoo(self):
...
def __str__(self):
return self.full_date
@dataclass
class MyDateTime:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def foo(self):
...
def goo(self):
...
def hoo(self):
...
def __str__(self):
return f"{self.year}, {self.month}, {self.day}"