Last Revision — April 19, 2022
2 Min Read
Attribute Name and Attributes Type are Opposite
Couplers
Names
Within a Class
- Primitive Obsession (co-exist)
- Uncommunicative Name (co-exist)
- Duplicated Code (causes)
William C. Wake in book (2004): "Refactoring Workbook"
Whenever a variable has an explicit type prefix or suffix, it can strongly signal that it should be just a class of its own. For example, current_date = "2021-14-11"
, embeds the potential class Date
in the name of a variable, and that could also be classified as the Primitive Obsession code smell.
Wake signals that the embedded type could also be in the method names, giving an example of a schedule.add_course(course)
function in contrast to schedule.add(course)
. He notes that it could have been a matter of preference, although he insists that this can be a problem wherever some generalization occurs [1]. When a parent class for Course
is introduced to cover not only courses but also series of courses, then add_course()
has a name that is no longer appropriate, thus suggesting the usage of more neutral terms. [1] Parameters of a method are part of the method name, and this kind of naming is also a duplication.
With all the possibilities of annotating variables, it is unnecessary to mention it twice through variable name when type annotation or type hinting is present. Names with embedded types in them, for which no class yet could represent them, are good candidates to be refactored into separate classes.
This was a standard in pointer-based languages, but it is not helpful in modern Object-Oriented Programming languages. [1]
Both the argument and name mentions the same type.
If the name of a variable is just precisely the name of the class, it's a case of Uncommunicative Name.
player_name: str = "Luzkan"
player_health: int = 100
player_stamina: int = 50
player_attack: int = 7
@dataclass
class Player:
stamina: int
health: int
attack: int
name: str
luzkan = Player(
name="Luzkan"
health=100,
stamina=50,
attack=7,
)
datetime = datetime.datetime.now()
foo()
datetime_2 = datetime.datetime.now()
foo_bench_start_time = datetime.datetime.now()
foo()
foo_bench_end_time = datetime.datetime.now()