Skip to main content

Command Palette

Search for a command to run...

Code Smell 90 - Implementative Callback Events

Published
2 min read
Code Smell 90 - Implementative Callback Events

When creating events, we should decouple the trigger from the action.

TL;DR: Name your functions according to what happened.

Problems

Solutions

  1. Name the events after "what happened", not "what you should do"

Sample Code

Wrong

const Item = ({name, handlePageChange)} =>
  <li onClick={handlePageChange}>
    {name}
  </li>

// handlePageChange is coupled with what you decide to do
/ /instead of what really happened
//
// We cannot reuse this kind of callback

Right

const Item = ({name, onItemSelected)} =>
  <li onClick={onItemSelected}>
    {name}
  </li>

// onItemSelected will be called just when an item was selected. KISS
// Parent can decide what to do (or do nothing)
// We defer the decision

Detection

This is a semantic smell. We can detect it on peer code reviews.

Tags

  • Coupling

  • Naming

Conclusion

Names are very important. We should delay implementation coupled names until the very last moment.

More Info

Credits

Photo by Ashim D’Silva on Unsplash

Thanks to Maciej for this tip


Beyond basic mathematical aptitude, the difference between good programmers and great programmers is verbal ability.

Marissa Mayer


This article is part of the CodeSmell Series.

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.