Temporary Field — Code Smells Catalog Skip to content

Temporary Field

Object Oriented Abusers Data Code Smell Within Class

A field that's null eleven months of the year and suddenly matters during one specific calculation. The object carries it everywhere, for one brief moment of relevance.

2 min read 1 source

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_date
PYTHON

Refactoring

  • Introduce Null Object
  • Extract Class
  • Move Function

Sources

Browse All 56 Smells