# Code Smell 109 - Automatic Properties

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

# Problems

- Information Hiding Violation

- [Mutability](https://maximilianocontieri.com/the-evil-powers-of-mutants)

- [Fail Fast Principle](https://maximilianocontieri.com/fail-fast) violation

- Duplicate code when setting properties

# Solutions

1. [Remove automatic setters and getters](https://maximilianocontieri.com/refactoring-001-remove-setters)

# 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

[Gist Url]: # (https://gist.github.com/mcsee/2353f11cfb336aaeda194c4a11a21324)
```csharp
class Person
{
  public string name 
  { get; set; }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/198d8a232bd1abf52cda0884fb96bc5f)
```csharp
class Person
{
  private string name  
  
  public Person(string personName)
  {
    name = personName;
    //imutable
    //no getters, no setters
  }
}
```

# 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

%[https://maximilianocontieri.com/code-smell-28-setters]

%[https://maximilianocontieri.com/code-smell-68-getters]

%[https://maximilianocontieri.com/code-smell-70-anemic-model-generators]

%[https://maximilianocontieri.com/code-smell-40-dtos]

%[https://maximilianocontieri.com/code-smell-01-anemic-models]

# More Info

- [W3 schools](https://www.w3schools.com/cs/cs_properties.php)

- %[https://maximilianocontieri.com/laziness-i-meta-programming]

- %[https://maximilianocontieri.com/lazyness-ii-code-wizards]

- %[https://maximilianocontieri.com/refactoring-001-remove-setters]

- %[https://maximilianocontieri.com/the-evil-powers-of-mutants]

- %[https://maximilianocontieri.com/fail-fast]

# Credits

Photo by <a href="https://unsplash.com/@konyxyzx">Kony</a> on <a href="https://unsplash.com/s/photos/shoot">Unsplash</a>
  
* * *

> Nothing is harder than working under a tight deadline and still taking the time to clean up as you go.

_Kent Beck_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
