Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 58 — Yo-yo Problem

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

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

Right

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

More info

https://wiki.c2.com/?DeepClassHierarchies

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

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response