Refactoring 009 - Protect Public Attributes

Refactoring 009 - Protect Public Attributes

Leader 1 47 113
calendar_todayschedule4 min read

Forget about data structures, DTOs, POJOs, and anemic objects.

TL;DR: Avoid external manipulation

Problems Addressed

  • Encapsulation Violation
  • Anemic Models

https://coderlegion.com/7246/code-smell-01-anemic-models

https://maximilianocontieri.com/code-smell-40-dtos

Context

Public attributes are a direct invitation to break encapsulation.

When you expose an object's internal state, you lose control over its consistency and lifecycle.

Any external consumer can modify its data without the object even knowing, leading to Anemic Models that are little more than glorified data structures.

You should force the interaction through a controlled interface.

This allows the object to maintain its invariants, validate state changes, and hide implementation details.

It transforms your code from a collection of leaked data into a set of robust, self-governing objects that truly own their behavior.

Steps

  1. 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 can't access attributes until we add methods

}

Type

[X] Semi-Automatic

You can change the visibility with an IDE or text editor.

Safety ️

This is not a safe refactor.

Existing dependencies may break.

Why is the Code Better? ✨

You can change encapsulated code easily.

The code is not repeated.

How Does it Improve the Bijection? ️

This refactoring improves the bijection between code and the real-world domain.

Public attributes violate the MAPPER principle by exposing internal implementation details.

When you protect the attributes, you reduce coupling and create a proper interface that reflects domain behavior rather than technical structure.

Limitations ⚠️

Some languages don't have visibility options.

Tags ️

  • Anemic Models

Level

[X] Intermediate

https://maximilianocontieri.com/refactoring-001-remove-setters

Refactor with AI

Suggested Prompt: 1. Identify all public attributes in the class.2. Change their visibility from public to private.3. Create appropriate getter and setter methods with proper validation

Without Proper Instructions With Specific Instructions
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
You You
Gemini Gemini
DeepSeek DeepSeek
Meta AI Meta AI
Grok Grok
Qwen Qwen

See also

Wikipedia

https://refactoring.guru/refactoring/techniques/dealing-with-generalization/hide-delegate

Credits

Image by Couleur on Pixabay


This article is part of the Refactoring Series.

https://maximilianocontieri.com/how-to-improve-your-code-with-easy-refactorings

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Why “Building in Public” Is Hollowing Out Your Developer Career

Karol Modelskiverified - Jun 18

Refactoring 012 - Reify Associative Arrays

Maxi Contieri - Jul 8

Refactoring 011 - Replace Comments with Tests

Maxi Contieri - Jun 17

Refactoring 010 - Extract Method Object

Maxi Contieri - Jun 9

Refactoring 008 - Convert Variables to Constant

Maxi Contieri - Mar 28
chevron_left
6.5k Points161 Badges
Buenos Aires, Argentinamaximilianocontieri.com
67Posts
3Comments
4Connections
Learn something new every day
Software Engineer and author of Clean Code Cookbook (https://amzn.to/4... Show more

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!