Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters

Avoid exposing mutable getters in your code to maintain object integrity and encapsulation. Use immutable copies or data structures to prevent unintended modifications and ensure thread safety.


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri

Using getters is a significant issue. Exposing internals is a major problem

TL;DR: Don't expose your internals and lose control

\

Problems

  • Mutability
  • Unexpected Changes
  • Ripple Effects
  • Thread unsafety
  • Encapsulation Principle violation

Solutions

  1. Return shallow copies of your collections

Context

Immutable objects are essential in functional and object-oriented programming.

Once created, their state cannot be altered.

\ This is key to keeping object integrity and ensuring thread safety in multithreaded applications.

\ Mutable getters allow callers to access and modify the internal state of an object, leading to potential corruption and unexpected behavior.

When you break encapsulation, you take responsibility away from an object. Integrity is lost.

\ Returning a page in a book is like an immutable copy. It cannot be edited, like a human memory.

You can edit some memories by bringing them from long-term memory.

Sample Code

Wrong

public class Person {
    private List<String> hobbies;

    public Person(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public List<String> getHobbies() {
        return hobbies;
    }
}
public class Person {
    private List<String> hobbies;

    public Person(List<String> hobbies) {
        this.hobbies = new ArrayList<>(hobbies);
    }

    public List<String> hobbies() {
        // This returns a shallow copy
        // This is usually not a big performance issue
        return new ArrayList<>(hobbies);
    }
}

Detection

  • [x] Semi-Automatic

You can detect mutable getters by examining the return types of your getters.

If they return mutable collections or objects, you need to refactor them to return immutable copies or use immutable data structures.

Tags

  • Mutability

Level

  • [x] Intermediate

AI Generation

AI generators might create this smell if they prioritize simplicity and brevity over best practices.

They not always consider the implications of returning mutable objects.

AI Detection

AI tools can detect this smell if you instruct them to look for getters returning mutable objects or collections.

They can suggest returning copies or using immutable types to fix the issue.

Conclusion

Getters are a code smell, but something you need to return objects you hold.

You can do it at your own risk, but retain the tracking of those collections.

Avoid mutable getters to protect your object integrity and encapsulation.

By returning immutable copies or using immutable types, you can prevent unintended modifications and ensure your objects remain reliable and predictable.

Relations

Code Smell 68 - Getters

Code Smell 109 - Automatic Properties

\

:::info More Info

The Evil Power of Mutants

Nude Models - Part II: Getters

:::

\

:::warning Disclaimer: Code Smells are my opinion.

:::

\

:::info Credits: Photo by Suzanne D. Williams on Unsplash

:::


The best programmers write only easy programs.

Michael A. Jackson

Software Engineering Great Quotes


:::tip This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

:::

\


This content originally appeared on HackerNoon and was authored by Maximiliano Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maximiliano Contieri | Sciencx (2024-06-30T16:46:44+00:00) Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters. Retrieved from https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/

MLA
" » Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters." Maximiliano Contieri | Sciencx - Sunday June 30, 2024, https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/
HARVARD
Maximiliano Contieri | Sciencx Sunday June 30, 2024 » Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters., viewed ,<https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/
CHICAGO
" » Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/
IEEE
" » Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/. [Accessed: ]
rf:citation
» Finding the Stinky Parts of Your Code: Code Smell 256 – Mutable Getters | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/06/30/finding-the-stinky-parts-of-your-code-code-smell-256-mutable-getters/ |

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.