Code Smell 255 - Parallel Hierarchies
Tags: webdev, beginners, programming, tutorial
Published: 2024-06-21 17:02:03

*Double Trouble: The Curse of Redundant Structures*

> TL;DR: Parallel hierarchies lead to duplication and tight coupling.

# Problems

- Increased complexity

- DRY / Code Duplication

- Maintenance Nightmare

- Coupling

- Ripple Effect

- Potential for inconsistencies across different hierarchies

# Solutions

1. Merge hierarchies

2. Use composition

3. Extract Common Functionality

# Refactorings

{% post https://dev.to/mcsee/refactoring-013-remove-repeated-code-4npi %}

{% post https://dev.to/mcsee/refactoring-007-extract-class-18ei %}

# Context

Parallel hierarchies occur when you must make a counterpart every time you create a domain class.

The counterpart might be persistence, UI, Controller, tests, Serialization, etc

This leads to duplicate structures and tight coupling. 

Changes in the domain model require changes in the parallel classes, making the system more brittle and harder to manage.

# Sample Code

## Wrong

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

```java
// Domain classes
abstract class Transaction {
    private String id;
    private double amount;
}

class BankTransaction extends Transaction {
    private String bankName;
}

class CreditCardTransaction extends Transaction {
    private String cardNumber;
}

// Persistence classes
abstract class TransactionDAO {
    private String id;
    private double amount;
}

class BankTransactionDAO extends TransactionDAO {
    private String bankName;
}

class CreditCardTransactionDAO extends TransactionDAO {
    private String cardNumber;
}
```

## Right

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

```java
public class TransactionService {
    private EntityManager entityManager;

    public TransactionService(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void saveTransaction(Transaction transaction) {
        entityManager.getTransaction().begin();
        entityManager.persist(transaction);
        entityManager.getTransaction().commit();
    }

    public Transaction loadTransaction(
        Long id, Class<? extends Transaction> transactionClass) {
        return entityManager.find(transactionClass, id);
    }
}
```

# Detection

[X] Semi-Automatic 

You can detect this smell by traversing the hierarchies

# Exceptions

- Some frameworks force you to extend your domain using this technique

# Tags

- Hierarchies

# Level

[X] Intermediate

# AI Generation

AI generators often create this smell by mirroring domain models in persistence layers without understanding the implications, leading to unnecessary duplication.

# AI Detection

AI Assistants can fix this smell with instructions to consolidate hierarchies and use composition, reducing duplication and improving maintainability.

ChatGPT offered a solution using **'Instanceof'** which is an even worse code smell

# Conclusion

Parallel hierarchies create unnecessary complexity and make the codebase harder to maintain. 

They bring [deep hierarchies](https://dev.to/mcsee/code-smell-137-inheritance-tree-too-deep-3j5p) which is a symptom of [subclassification for code reuse](https://dev.to/mcsee/code-smell-11-subclassification-for-code-reuse-1136)

You can merge the hierarchies and use composition to simplify the design and improve the system's robustness. 

You can use Metaprogramming to manage the persistence or the unit tests.

[Metaprogramming](https://dev.to/mcsee/laziness-i-meta-programming-32a9) is also a code smell when you use it for domain problems, but persistence and testing are orthogonal domains.

# Relations

{% post https://dev.to/mcsee/code-smell-137-inheritance-tree-too-deep-3j5p %}

{% post https://dev.to/mcsee/code-smell-11-subclassification-for-code-reuse-1136 %}

{% post https://dev.to/mcsee/code-smell-58-yo-yo-problem-ej9 %}

# More Info

{% post https://dev.to/mcsee/laziness-i-meta-programming-32a9 %}

# 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

Foto de <a href="https://unsplash.com/@artisanalphoto">ArtisanalPhoto</a> en <a href="https://unsplash.com/fotos/barandillas-de-metal-gris-en-escalera-blanca-MJcb7ZhNeUA">Unsplash</a>
    
* * *

> Inheritance is surely a good answer but who knows the questions?

_Michel Gauthier_
 
{% 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 %}