Dev Genius

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

Follow publication

Code Smell 90 — Implementative Callback Events

Photo by Ashim D’Silva on Unsplash

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

TL;DR: Name your functions acording 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 to what you decide to do
//instead of what really happened
//
//We cannot reuse this kind of callbacks

Right

const Item = ({name, onItemSelected)} =>
<li onClick={onItemSelected}>
{name}
</li>
//onItemSelected will be called just when a 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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

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

Responses (1)

Write a response

Absolutely, spot on.

--