Overview
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.
Problems
🧠
Hard to Understand
Additional fields clutter the code and strain the cognitive load by keeping in mind useless attributes.
Example
1@dataclass
2class MyDateTime:
3 def __init__(self, year, month, day):
4 self.year = year
5 self.month = month
6 self.day = day
7 self.full_date = f"{year}, {month}, {day}"
8
9 def foo(self):
10 ...
11
12 def goo(self):
13 ...
14
15 def hoo(self):
16 ...
17
18 def __str__(self):
19 return self.full_dateRefactoring
- Introduce Null Object
- Extract Class
- Move Function
Sources
- ORIGIN1999 · ISBN 978-0201485677