cover-img

Code Smell 58 - Yo-yo Problem

Searching for a concrete method implementation? Go back and forth, up and down.

31 March, 2023

2

2

0

Searching for a concrete method implementation? Go back and forth, up and down.

TL;DR: Don't ab(use) hierarchies.

Problems

  • Deep Hierarchies
  • Subclassification for Code Reuse
  • Readability
  • Low Cohesion
  • High Coupling

Solutions

  1. Favor composition over inheritance.
  2. Refactor deep hierarchies.

Sample Code

Wrong

<?

abstract class Controller {

}

class BaseController extends Controller {

}

class SimpleController extends BaseController {

}

class ControllerBase extends SimpleController {

}

class LoggedController extends ControllerBase {

}

class RealController extends LoggedController {

}

Right

<?

interface ControllerInterface {

}

abstract class Controller implements ControllerInterface {

}

final class LoggedControllerDecorator implements ControllerInterface {

}

final class RealController implements ControllerInterface {

}

Detection

Any linter can check for suspects against a max depth threshold.

Tags

  • Hierarchy

Conclusion

Many novice programmers reuse code through hierarchies. This brings high coupled and low cohesive hierarchies.

Johnson and Foote established in their paper this was actually a good design recipe back in 1988. We have learned a lot from there.

We must refactor and flatten those classes.

Relations

Code Smell 11 - Subclassification for Code Reuse

https://maximilianocontieri.com/code-smell-137-inheritance-tree-too-deep

More Info

Coupling - The one and only software design problem

c2

Hierarchies should be deep by Johnson and Foote


An error arises from treating object variables (instance variables) as if they were data attributes and then creating your hierarchy based on shared attributes. Always create hierarchies based on shared behaviors, side.

David West


Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code


2

2

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.