BillionaireClubCollc
  • News
  • Notifications
  • Shop
  • Cart
  • Media
  • Advertise with Us
  • Profile
  • Groups
  • Games
  • My Story
  • Chat
  • Contact Us
home shop notifications more
Signin
  •  Profile
  •  Sign Out
Skip to content

Billionaire Club Co LLC

Believe It and You Will Achieve It

Primary Menu
  • Home
  • Politics
  • TSR
  • Anime
  • Michael Jordan vs.Lebron James
  • Crypto
  • Soccer
  • Dating
  • Airplanes
  • Forex
  • Tax
  • New Movies Coming Soon
  • Games
  • CRYPTO INSURANCE
  • Sport
  • MEMES
  • K-POP
  • AI
  • The Bahamas
  • Digital NoMad
  • Joke of the Day
  • RapVerse
  • Stocks
  • SPORTS BETTING
  • Glamour
  • Beauty
  • Travel
  • Celebrity Net Worth
  • TMZ
  • Lotto
  • COVD-19
  • Fitness
  • The Bible is REAL
  • OutDoor Activity
  • Lifestyle
  • Culture
  • Boxing
  • Food
  • LGBTQ
  • Poetry
  • Music
  • Misc
  • Open Source
  • NASA
  • Science
  • Natural & Holstict Med
  • Gardening
  • DYI
  • History
  • Art
  • Education
  • Pets
  • Aliens
  • Astrology
  • Farming and LiveStock
  • LAW
  • Fast & Furious
  • Fishing & Hunting
  • Health
  • Credit Repair
  • Grants
  • All things legal
  • Reality TV
  • Africa Today
  • China Today
  • "DUMB SHIT.."
  • CRYPTO INSURANCE

Code Smell 255 - Addressing Parallel Hierarchies in Code

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

Merge hierarchies
Use composition
Extract Common Functionality

Refactorings
https://hackernoon.com/refactoring-013-eliminating-repeated-code-with-dry-principles?embedable=true
https://maximilianocontieri.com/refactoring-007-extract-class?embedable=true
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
Code Smell 137 - Inheritance Tree Too Deep
Code Smell 11 - Subclassification for Code Reuse
Code Smell 58 - Yo-yo Problem
More Info
https://hackernoon.com/laziness-chapter-i-meta-programming-6s4l300e?embedable=true
Disclaimer
Code Smells are my opinion.

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

Michel Gauthier
Software Engineering Great Quotes

This article is part of the CodeSmell Series.
How to Find the Stinky Parts of your Code

Welcome to Billionaire Club Co LLC, your gateway to a brand-new social media experience! Sign up today and dive into over 10,000 fresh daily articles and videos curated just for your enjoyment. Enjoy the ad free experience, unlimited content interactions, and get that coveted blue check verification—all for just $1 a month!

Source link

Share
What's your thought on the article, write a comment
0 Comments
×

Sign In to perform this Activity

Sign in
×

Account Frozen

Your account is frozen. You can still view content but cannot interact with it.

Please go to your settings to update your account status.

Open Profile Settings

Ads

  • Billionaire128 Liquid Gold Drawstring bag

    $ 22.50
  • Billionaire128 Liquid Gold Laptop Sleeve

    $ 28.00
  • Original Billionaire128 Old School Bucket Hat

    $ 28.50
  • News Social

    • Facebook
    • Twitter
    • Facebook
    • Twitter
    Copyright © 2024 Billionaire Club Co LLC. All rights reserved