Code Smell 256 - Mutable Getters
Tags: webdev, beginners, programming, java
Published: 2024-06-29 23:49:05

*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](https://dev.to/mcsee/the-evil-powers-of-mutants-1noo) 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

[Gist Url]: # (https://gist.github.com/mcsee/715a932cd775b89b1ea04ce0e42775fe)

```java
public class Person {
    private List<String> hobbies;

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

    public List<String> getHobbies() {
        return hobbies;
    }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/ddb3f75add70512e671e57a9440a862c)

```java
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](https://dev.to/mcsee/code-smell-68-getters-o12), 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

{% post https://dev.to/mcsee/code-smell-68-getters-o12 %}

{% post https://dev.to/mcsee/code-smell-109-automatic-properties-f40 %}

# More Info

{% post https://dev.to/mcsee/the-evil-powers-of-mutants-1noo %}

{% post https://dev.to/mcsee/nude-models-part-ii-getters-3f2a %}

# Disclaimer

Code Smells are my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Suzanne D. Williams](https://unsplash.com/es/@scw1217) on [Unsplash](https://unsplash.com/s/photos/tres-pupas-VMKBFR6r_jg)  
  
* * *

> The best programmers write only easy programs.

_Michael A. Jackson_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}