attilagyorffy.com

Convention to use distinctive callback names in async.waterfall

Introduction

Attila Györffy

Attila Györffy

I'm a Ruby/JS dev/trainer with a focus on quality. An ex-Londoner, @terracycle, @ubxd, @lastfm. Follow me at http://twitter.com/attilagyorffy


node.js javascript convention communication

Convention to use distinctive callback names in async.waterfall

Posted by Attila Györffy on .
Featured

node.js javascript convention communication

Convention to use distinctive callback names in async.waterfall

Posted by Attila Györffy on .

I've come across quite a few node projects lately that use the async.waterfall method to execute a series of callbacks in a linear fashion. In some cases the async.waterfall call is nested in a function body that has its own callback. I see code like this:

Notice that both the main doSomething function and the functions in async.waterfall have the same callback names, referenced as cb. Even though node's javascript compiler will correctly call the right callback based on its reference lookup, this piece of code is error prone to humans.

In order to have distinctive callback names, I propose the following naming convention:

  • Parent function callbacks are called callback (not cb, those extra 6 characters won't hurt your fingers but will help people)
  • Callbacks that are used with async.waterfall are called next in order to express linear (sync) execution.

Essentially my example above becomes the following:

The goal of the above is to indicate linear execution and to introduce a convention to distinguish between nested callbacks. In case there's an already existing convention, please leave a message as I'd like to know.

In order to get as many people on board with expressive conventions as possible and to make things easier for every single one of us in the whole community, please consider spreading the word.

Attila Györffy

Attila Györffy

https://attilagyorffy.com

I'm a Ruby/JS dev/trainer with a focus on quality. An ex-Londoner, @terracycle, @ubxd, @lastfm. Follow me at http://twitter.com/attilagyorffy

View Comments...