Code Smell 255 – Parallel Hierarchies

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 incons…


This content originally appeared on DEV Community and was authored by Maxi Contieri

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

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

// 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

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 which is a symptom of subclassification for code reuse

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 is also a code smell when you use it for domain problems, but persistence and testing are orthogonal domains.

Relations

More Info

Disclaimer

Code Smells are my opinion.

Credits

Foto de ArtisanalPhoto en Unsplash

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

Michel Gauthier

This article is part of the CodeSmell Series.


This content originally appeared on DEV Community and was authored by Maxi Contieri


Print Share Comment Cite Upload Translate Updates
APA

Maxi Contieri | Sciencx (2024-06-21T17:02:03+00:00) Code Smell 255 – Parallel Hierarchies. Retrieved from https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/

MLA
" » Code Smell 255 – Parallel Hierarchies." Maxi Contieri | Sciencx - Friday June 21, 2024, https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/
HARVARD
Maxi Contieri | Sciencx Friday June 21, 2024 » Code Smell 255 – Parallel Hierarchies., viewed ,<https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/>
VANCOUVER
Maxi Contieri | Sciencx - » Code Smell 255 – Parallel Hierarchies. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/
CHICAGO
" » Code Smell 255 – Parallel Hierarchies." Maxi Contieri | Sciencx - Accessed . https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/
IEEE
" » Code Smell 255 – Parallel Hierarchies." Maxi Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/. [Accessed: ]
rf:citation
» Code Smell 255 – Parallel Hierarchies | Maxi Contieri | Sciencx | https://www.scien.cx/2024/06/21/code-smell-255-parallel-hierarchies/ |

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.