Forget about data structures, DTOs, POJOs, and anemic objects.
TL;DR: Avoid external manipulation
Problems Addressed
Encapsulation Violation
Anemic Models
Related Code Smells
![mcsee](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366059%2F1d8942bc-727d-4d1e-bc50-30ff073a34fc.jpeg)
Code Smell 01 - Anemic Models
Maxi Contieri ・ Oct 20 '20
Steps
- Change the visibility of your attributes from public to private.
Sample Code
Before
public class Song {
String artistName;
String AlbumName;
}
After
public class Song {
// 1- Change the visibility of your attributes from public to private
private String artistName;
private String AlbumName;
// We cannot access attributes until we add methods
}
Type
[X] Semi-Automatic
We can change the visibility with an IDE or text editor.
Safety
This is not a safe refactor.
Existing dependencies may break.
Why code is better?
We can change encapsulated code easily.
The code is not repeated.
Limitations
Some languages don't have visibility options.
Tags
- Anemic
Related Refactorings
![mcsee](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F366059%2F1d8942bc-727d-4d1e-bc50-30ff073a34fc.jpeg)
Refactoring 001 - Remove Setters
Maxi Contieri ・ Nov 17 '21
Credits
This article is part of the Refactoring Series.
Top comments (0)