Code Smell 78 — Callback Hell

Processing an algorithm as a sequence of nested callbacks is not clever.

Maximiliano Contieri
Dev Genius

--

TL;DR: Don’t process calls in a callback way. Write a sequence.

Problems

  • Readability
  • Hard to debug.
  • Complexity

Solutions

1. Change callbacks to sequence calls.

2. Extract repeated Code

3. Refactor.

Sample Code

Wrong

Right

Detection

This problem shines at the naked eye. Many linters can detect this complexity and warn us.

Tags

  • Readability
  • Complexity

Conclusion

Callback Hell is a very common problem in programming languages with futures or promises.

Callbacks are added in an incremental way. There’s no much mess at the beginning.

Complexity without refactoring makes them hard to read and debug.

Relations

There are two ways to write code: write code so simple there are obviously no bugs in it, or write code so complex that there are no obvious bugs in it.

Tony Hoare

--

--

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