# Code Smell 266 - Collection Aliasing

> TL;DR: Use immutable collections to prevent unintended side effects.

# Problems

- Unpredictable behavior
- Debugging challenges
- Data corruption
- Violation of the Principle of Least Astonishment
- Premature optimization
- Unexpected [Mutations](https://maximilianocontieri.com/the-evil-powers-of-mutants)
- Concurrency problems
- Compromised thread safety
- Increased [coupling](https://maximilianocontieri.com/coupling-the-one-and-only-software-design-problem)

# Solutions

1. Use immutable collections
2. Create immutable classes
3. Copy the collection before modification
4. Avoid [collection getters](https://maximilianocontieri.com/code-smell-256-mutable-getters)
5. Avoid [automatic properties](https://maximilianocontieri.com/code-smell-109-automatic-properties)
6. Favor information hiding and encapsulation

# Context

Aliasing happens when two or more variables refer to the same object. 

This can lead to unexpected side effects, especially when one variable modifies the shared object. 

You can't change immutable collections after creation helping you prevent accidental aliasing.

Premature optimizators will argue that copying collections is an expensive operation that you should avoid.

This is a special case of Object Aliasing

# Sample Code

## Wrong

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

```java
public class MutableExample {
  public static void main(String[] args) {
    List<Integer> numbers = List.of(1, 2, 3);
    List<Integer> otherNumbers = numbers; // Aliasing
    otherNumbers.add(4);
    System.out.println(numbers); // Output: [1, 2, 3, 4]
  }
}
```

## Right

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

```java
public class ImmutableExample {
  public static void main(String[] args) {
    List<Integer> numbers = List.of(1, 2, 3);
    List<Integer> otherNumbers = List.copyOf(numbers); // Creating a copy
    otherNumbers.add(4);
    System.out.println(numbers); // Output: [1, 2, 3]
  }
}
```

# Detection

[X] Semi-Automatic 

Several static analysis tools can warn you of aliasing abuse.

# Tags

- Immutability

# Level

[x] Intermediate

# AI Generation

AI code generators might not always create immutable objects by default, especially when working with mutable collections.

You can prompt them to prioritize immutable collections and wrap existing ones to avoid aliasing.

# AI Detection

AI tools can analyze code for potential aliasing issues and suggest using immutable collections instead.

# Conclusion

You can avoid unintended side effects using immutable collections.

This will make your code more predictable and easier to reason about.

# Relations

%[https://maximilianocontieri.com/code-smell-86-mutable-const-arrays]

%[https://maximilianocontieri.com/code-smell-127-mutable-constants]

%[https://maximilianocontieri.com/code-smell-256-mutable-getters]

%[https://maximilianocontieri.com/code-smell-109-automatic-properties]

%[https://maximilianocontieri.com/code-smell-68-getters]

# More Info

%[https://maximilianocontieri.com/the-evil-powers-of-mutants]

%[https://maximilianocontieri.com/nude-models-part-ii-getters]

%[https://maximilianocontieri.com/coupling-the-one-and-only-software-design-problem]

# Disclaimer

Code Smells are my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

# Credits

Photo by [Martino Pietropoli](https://unsplash.com/@martino_pietropoli) on [Unsplash](https://unsplash.com/photos/woman-walking-with-shadow-pirWeToS2mA)
    
* * *

> If an object is immutable, it can be in only one state, and you win big..

_Joshua Bloch_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
