Skip to content

Latest commit

 

History

History
127 lines (72 loc) · 4.18 KB

File metadata and controls

127 lines (72 loc) · 4.18 KB

Code Smell 109 - Automatic Properties

Code Smell 109 - Automatic Properties

What happens if you combine 4 code smells?

TL;DR: Avoid Getters, Avoid Setters, Avoid Metaprogramming. Think about Behavior.

Problems

Solutions

  1. Remove automatic setters and getters

Context

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.

Sample Code

Wrong

class Person
{
  public string name 
  { get; set; }
}

Right

class Person
{
  private string name;  
  
  public Person(string personName)
  {
    name = personName;
    // immutable
    // no getters, no setters
  }

  // ... more protocol, probably accessing private variable name
}

Detection

[X] Automatic

This is a language feature.

We should avoid immature languages or forbid their worst practices.

Tags

  • Encapsulation

Conclusion

We need to think carefully before exposing our properties.

The first step is to stop thinking about properties and focus solely on behavior.

Relations

Code Smell 28 - Setters

Code Smell 68 - Getters

Code Smell 70 - Anemic Model Generators

Code Smell 40 - DTOs

Code Smell 01 - Anemic Models

Code Smell 190 - Unnecessary Properties

More Info

W3 schools

Laziness I - Metaprogramming

Laziness II - Code Wizards

Refactoring 001 - Remove Setters

The Evil Power of Mutants

Fail Fast

Credits

Photo by Kony on Unsplash


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.

How to Find the Stinky Parts of your Code