What happens if you combine 4 code smells?
TL;DR: Avoid Getters, Avoid Setters, Avoid Metaprogramming. Think about Behavior.
-
Information Hiding Violation
-
Fail Fast Principle violation
-
Duplicate code when setting properties
Setters and getters are a bad industry practice.
Many IDEs favor this code smell.
Some languages provide explicit support to build anemic models and DTOs.
class Person
{
public string name
{ get; set; }
}
class Person
{
private string name;
public Person(string personName)
{
name = personName;
// immutable
// no getters, no setters
}
// ... more protocol, probably accessing private variable name
}
[X] Automatic
This is a language feature.
We should avoid immature languages or forbid their worst practices.
- Encapsulation
We need to think carefully before exposing our properties.
The first step is to stop thinking about properties and focus solely on behavior.
Code Smell 70 - Anemic Model Generators
Code Smell 190 - Unnecessary Properties
Refactoring 001 - Remove Setters
Nothing is harder than working under a tight deadline and still taking the time to clean up as you go.
Kent Beck
Software Engineering Great Quotes
This article is part of the CodeSmell Series.