Code Smell - Collection Aliasing
Exposing your collections couples your solution
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
- Concurrency problems
- Compromised thread safety
- Increased coupling
Solutions
- Use immutable collections
- Create immutable classes
- Copy the collection before modification
- Avoid collection getters
- Avoid automatic properties
- 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
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
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

Code Smell 86 - Mutable Const Arrays
Maxi Contieri ・ Aug 25 '21

Code Smell 127 - Mutable Constants
Maxi Contieri ・ Apr 5 '22

Code Smell 256 - Mutable Getters
Maxi Contieri ・ Jun 29

Code Smell 109 - Automatic Properties
Maxi Contieri ・ Dec 7 '21

Code Smell 68 - Getters
Maxi Contieri ・ Apr 29 '21
More Info

The evil powers of mutants
Maxi Contieri ・ Dec 19 '20

Nude Models — Part II : Getters
Maxi Contieri ・ Feb 23 '21

Coupling: The one and only software design problem
Maxi Contieri ・ Feb 6 '21
Disclaimer
Code Smells are my opinion.
Credits
Photo by Martino Pietropoli on Unsplash
If an object is immutable, it can be in only one state, and you win big.
Joshua Bloch

Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
The above is the detailed content of Code Smell - Collection Aliasing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
