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

Information Hiding Violation
Mutability
Fail Fast Principle violation
Duplicate code when setting prop…


This content originally appeared on DEV Community and was authored by Maxi Contieri

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;
    //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

More Info

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

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2021-12-07T03:01:02+00:00) Code Smell 109 – Automatic Properties. Retrieved from https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/

MLA
" » Code Smell 109 – Automatic Properties." Maxi Contieri | Sciencx - Tuesday December 7, 2021, https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/
HARVARD
Maxi Contieri | Sciencx Tuesday December 7, 2021 » Code Smell 109 – Automatic Properties., viewed ,<https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 109 – Automatic Properties. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/
CHICAGO
" » Code Smell 109 – Automatic Properties." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/
IEEE
" » Code Smell 109 – Automatic Properties." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/. [Accessed: ]
rf:citation
» Code Smell 109 – Automatic Properties | Maxi Contieri | Sciencx | https://www.scien.cx/2021/12/07/code-smell-109-automatic-properties/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.