Course contentsShow
← JavaScript
Lesson 1853 of 2,72338. Object-Oriented PatternsFree lesson

The Yo-Yo Problem: Navigating Complex Hierarchies

Recognize when jumping between parent and child classes makes code hard to follow.

The Yo-Yo Problem: Navigating Complex Hierarchies

You'll learn how constantly jumping up and down an inheritance chain makes code difficult to understand and maintain.

What Is the Yo-Yo Problem?

The "yo-yo problem" occurs when you have to repeatedly bounce between parent and child classes to understand what a single method actually does. Like a yo-yo going up and down, your eyes (and mental model) must travel up the inheritance chain to find a method definition, then back down to see how it's overridden, then back up again to find another piece, and so on.

This happens most often in deep inheritance hierarchies where:

  • Parent classes call methods that child classes override
  • Child classes call super methods that themselves call other overridable methods
  • Logic is split across multiple levels with no clear responsibility

Why It's Problematic

When reading code, you might see:

class Animal { 
  move() { this.makeSound(); }
}
class Dog extends Animal { 
  makeSound() { super.move(); /* ... */ }
}

Wait—Dog.makeSound() calls super.move(), which calls this.makeSound() again? You're yo-yoing between classes just to trace execution flow.

In deep hierarchies (remember those 5+ level chains?), this becomes exponentially worse. You lose track of which version of a method runs, what this refers to, and where side effects occur.

The Solution

Favor shallow hierarchies and composition over deep inheritance. Keep related logic in one place. If you find yourself constantly scrolling between files to understand behavior, it's a sign the hierarchy is too complex.

Key Takeaway: The yo-yo problem makes code unreadable by forcing you to constantly jump between parent and child classes to understand execution flow—a clear signal to flatten your hierarchy or use composition instead.