# Code Smell 267 - Objects Aliasing

> TL;DR: Use immutable objects to prevent unexpected changes caused by aliasing.

# Problems

- Unexpected [mutations](https://maximilianocontieri.com/the-evil-powers-of-mutants)
- Difficult bug tracking
- Unpredictable code behavior
- Reduced code predictability
- Increased coupling
- Compromised thread safety

# Solutions

1. Use immutable objects
2. Implement defensive copying
3. Favor functional programming

# Refactorings

%[https://maximilianocontieri.com/refactoring-008-convert-variables-to-constant]

# Context

Aliasing happens when multiple references point to the same mutable object. 

This can lead to unexpected changes in them when one part of the code modifies the object, affecting all references. 

Immutable objects mitigate this risk by ensuring you cannot change their internal representation once you create an object.

[Collection Aliasing](https://maximilianocontieri.com/code-smell-266-collection-aliasing) is a notable example of this issue.

# Sample Code

## Wrong

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

```java
public class Person {
  private String name; 
}

public void modifyPerson(Person person) {
  person.setName("Cosmo Kramer");
}

public static void main(String[] args) {
  Person p1 = new Person("Newman");
  Person p2 = p1; // p1 and p2 refer to the same object

  modifyPerson(p1);

  System.out.println(p1.name()); // Output: Cosmo Kramer
  System.out.println(p2.name()); // Output: Cosmo Kramer (unexpected)
}
```

## Right

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

```java
public class ImmutablePerson {
  private final String name; 

  public ImmutablePerson(String name) {
    this.name = name; 
  } 
}

public ImmutablePerson withName(String newName) {
    return new ImmutablePerson(newName);
}

public static void main(String[] args) {
  ImmutablePerson p1 = new ImmutablePerson("Newman");
  ImmutablePerson p2 = p1; // p1 and p2 refer to the same object

  // Modifying p1 creates a new object
  ImmutablePerson p3 = p1.withName("Cosmo Kramer");
  // but this is a bad practice 
  // since only constructors should create new objects
  // A better option is
  ImmutablePerson p3 = new ImmutablePerson("Cosmo Kramer");

  System.out.println(p1.name()); // Output: Newman
  System.out.println(p2.name()); // Output: Newman
  System.out.println(p3.name()); // Output: Cosmo Kramer
}
```

# Detection

[X] Semi-Automatic 

You can detect this smell by reviewing your code for mutable objects shared across different parts of your program.

# Tags

- Mutability

# Level

[x] Intermediate

# AI Generation

AI generators might introduce this smell if they're not specifically trained to prioritize immutability and avoid aliasing issues.

# AI Detection

AI detectors identify this smell by analyzing code for mutable shared objects and suggesting immutable alternatives. 

They need specific instructions on the context and the importance of immutability in the codebase.

## Try Them!

*Remember AI Assistants make lots of mistakes*

 [ChatGPT](https://chat.openai.com/?q=Correct+this+code%3A+public+class+Person+%7B+private+String+name%3B+%7D+public+void+modifyPerson%28Person+person%29+%7B+person.setName%28%22Cosmo+Kramer%22%29%3B+%7D+public+static+void+main%28String%5B%5D+args%29+%7B+Person+p1+%3D+new+Person%28%22Newman%22%29%3B+Person+p2+%3D+p1%3B+%2F%2F+p1+and+p2+refer+to+the+same+object+modifyPerson%28p1%29%3B+System.out.println%28p1.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+System.out.println%28p2.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+%28unexpected%29+%7D)	 [Claude](https://claude.ai/new?q=Correct+this+code%3A+public+class+Person+%7B+private+String+name%3B+%7D+public+void+modifyPerson%28Person+person%29+%7B+person.setName%28%22Cosmo+Kramer%22%29%3B+%7D+public+static+void+main%28String%5B%5D+args%29+%7B+Person+p1+%3D+new+Person%28%22Newman%22%29%3B+Person+p2+%3D+p1%3B+%2F%2F+p1+and+p2+refer+to+the+same+object+modifyPerson%28p1%29%3B+System.out.println%28p1.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+System.out.println%28p2.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+%28unexpected%29+%7D)
 [Perplexity](https://perplexity.ai/?q=Correct+this+code%3A+public+class+Person+%7B+private+String+name%3B+%7D+public+void+modifyPerson%28Person+person%29+%7B+person.setName%28%22Cosmo+Kramer%22%29%3B+%7D+public+static+void+main%28String%5B%5D+args%29+%7B+Person+p1+%3D+new+Person%28%22Newman%22%29%3B+Person+p2+%3D+p1%3B+%2F%2F+p1+and+p2+refer+to+the+same+object+modifyPerson%28p1%29%3B+System.out.println%28p1.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+System.out.println%28p2.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+%28unexpected%29+%7D)
 [Gemini](https://gemini.google.com/?q=Correct+this+code%3A+public+class+Person+%7B+private+String+name%3B+%7D+public+void+modifyPerson%28Person+person%29+%7B+person.setName%28%22Cosmo+Kramer%22%29%3B+%7D+public+static+void+main%28String%5B%5D+args%29+%7B+Person+p1+%3D+new+Person%28%22Newman%22%29%3B+Person+p2+%3D+p1%3B+%2F%2F+p1+and+p2+refer+to+the+same+object+modifyPerson%28p1%29%3B+System.out.println%28p1.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+System.out.println%28p2.name%28%29%29%3B+%2F%2F+Output%3A+Cosmo+Kramer+%28unexpected%29+%7D)

# Conclusion

Using immutable objects and avoiding aliasing can significantly improve your code's predictability, reduces bugs, and improves thread safety. 

It requires a shift in thinking and the benefits of immutability far outweigh the initial learning curve.

# Relations

%[https://maximilianocontieri.com/code-smell-176-changes-in-essence]

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

%[https://maximilianocontieri.com/code-smell-266-collection-aliasing]

# More Info

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

# 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 [Natural Photos](https://unsplash.com/@naturalphotos08) on [Unsplash](https://unsplash.com/photos/a-man-standing-in-front-of-a-display-of-key-chains-eWXLPRjaoRk)  
  
* * *

> Immutability changes everything.

_Pat Helland_

%[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]
