Code Smell 267 – Objects Aliasing

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.


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

Favor immutability to retain control of your objects

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

Problems

  • Unexpected mutations
  • 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?embedable=true

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 is a notable example of this issue.

Sample Code

Wrong

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)
}
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.

Tag(s)

  • 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 Claude Perplexity Gemini

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.

Related Reading

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxxvi

https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xxvi

https://hackernoon.com/code-smell-266-collection-aliasing?embedable=true

\ https://hackernoon.com/is-it-crystal-clear-for-everybody-that-a-date-should-not-mutate-wuoy3z03?embedable=true

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

:::

:::info Credits: Photo by Natural Photos on Unsplash

:::


Immutability changes everything.

Pat Helland

https://hackernoon.com/400-thought-provoking-software-engineering-quotes?embedable=true


:::tip This article is part of the CodeSmell Series on HackerNoon: 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-09-02T11:41:49+00:00) Code Smell 267 – Objects Aliasing. Retrieved from https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/

MLA
" » Code Smell 267 – Objects Aliasing." Maximiliano Contieri | Sciencx - Monday September 2, 2024, https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/
HARVARD
Maximiliano Contieri | Sciencx Monday September 2, 2024 » Code Smell 267 – Objects Aliasing., viewed ,<https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 267 – Objects Aliasing. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/
CHICAGO
" » Code Smell 267 – Objects Aliasing." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/
IEEE
" » Code Smell 267 – Objects Aliasing." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/. [Accessed: ]
rf:citation
» Code Smell 267 – Objects Aliasing | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/09/02/code-smell-267-objects-aliasing-2/ |

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.