Code Smell 271 – The Hollywood Principle

The [Hollywood Principle] is a software design principle emphasizing loose coupling between components. High-level components should not directly control the execution flow in low-level code. The code is tightly coupled to specific payment method classes like CreditCardProcessor and CryptoService. This is also known as *Inversion of Control*.


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

Don't Call Us, We'll Call You

TL;DR: The Hollywood Principle promotes loose coupling by inverting control. High-level components decide when and how to use low-level components.

Problems

  • Tight coupling
  • Difficult to extend
  • Reduced flexibility
  • Increased complexity
  • Violation of SOLID principles
  • Lack of Testability

Solutions

  1. Apply Inversion of control

    \

  2. Use Dependency injection

    \

  3. Depend on abstractions

Context

The Hollywood Principle is a software design principle emphasizing loose coupling between components.

\ High-level components should not directly control the execution flow in low-level components.

\ Low-level components should register themselves with high-level components, and high-level components should decide when and how to use them.

\ This is also known as Inversion of Control.

Sample Code

Wrong

class TicketCart {
    private paymentMethod: PaymentMethod;

    constructor(paymentMethodType: string) {
        // TicketCart is tightly coupled
        // to specific payment method classes 
        // like CreditCardProcessor and CryptoService.  
        if (paymentMethodType === 'creditCard') {
            this.paymentMethod = new CreditCardProcessor();
        } else if (paymentMethodType === 'Crypto') {
            this.paymentMethod = new CryptoService();
        } else {
            throw new Error('Invalid payment method');
        }
    }

    checkout(money: Money): void {
        this.paymentMethod.pay(money);
    }
}

const cart = new TicketCart('creditCard');
const money = new Money(126, 'USD');
cart.checkout(money);
interface PaymentMethod {
    pay(total: Money): void;
}

class TicketCart {
    private paymentMethod: PaymentMethod;

    constructor(paymentMethod: PaymentMethod) {
        // This solution is more open and less coupled
        // because it relies on abstractions
        this.paymentMethod = paymentMethod;
    }

    checkout(total: Money): void {
        this.paymentMethod.pay(total);
    }
}

class CreditCardProcessor implements PaymentMethod {
    pay(total: Money): void {
        console.log(`Processing payment of ${total.Amount()} 
        ${total.currency()} using credit card.`);
    }
}

const creditCardProcessor = new CreditCardProcessor();
const cart = new TicketCart(creditCardProcessor);
const total = new Money(126, 'USD');
cart.checkout(total);

Detection

  • [x] Manual

This is a design smell

Tags

  • Coupling

Level

[X ] Intermediate

AI Generation

AI generators can sometimes create code that violates the Hollywood Principle if you don't explicitly instruct them to follow inversion of control patterns.

\ They often generate straightforward, tightly coupled code by default.

AI Detection

AI tools can effectively detect violations of the Hollywood Principle by analyzing code dependencies and identifying tight coupling with proper instructions. (see below).

Try Them!

Remember: AI Assistants make lots of mistakes

| Without Proper Instructions | With Specific Instructions | |----|----| | ChatGPT | ChatGPT | | Claude | Claude | | Perplexity | Perplexity | | Copilot | Copilot | | Gemini | Gemini |

Conclusion

This principle can improve your code quality, reduce complexity, and enhance testability.

Relations

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

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

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

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

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

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

More Info

https://hackernoon.com/coupling-the-one-and-only-software-designing-problem-9z5a321h?embedable=true

https://en.wikipedia.org/wiki/Inversionofcontrol?embedable=true

https://en.wiktionary.org/wiki/Hollywood_principle?embedable=true

Disclaimer

Code Smells are my opinion.


"Dependency Injection is fundamentally about passing dependencies to objects, rather than having objects create or find them."

Martin Fowler

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


This article is part of the CodeSmell Series.

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-25T22:51:21+00:00) Code Smell 271 – The Hollywood Principle. Retrieved from https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/

MLA
" » Code Smell 271 – The Hollywood Principle." Maximiliano Contieri | Sciencx - Wednesday September 25, 2024, https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/
HARVARD
Maximiliano Contieri | Sciencx Wednesday September 25, 2024 » Code Smell 271 – The Hollywood Principle., viewed ,<https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/>
VANCOUVER
Maximiliano Contieri | Sciencx - » Code Smell 271 – The Hollywood Principle. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/
CHICAGO
" » Code Smell 271 – The Hollywood Principle." Maximiliano Contieri | Sciencx - Accessed . https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/
IEEE
" » Code Smell 271 – The Hollywood Principle." Maximiliano Contieri | Sciencx [Online]. Available: https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/. [Accessed: ]
rf:citation
» Code Smell 271 – The Hollywood Principle | Maximiliano Contieri | Sciencx | https://www.scien.cx/2024/09/25/code-smell-271-the-hollywood-principle/ |

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.