Elegant patterns in modern JavaScript RORO

Click for: original source

Bill Sourour from DevMastery.com wrote this article to introduce the pattern he has been using. He claims he did not invent it and came across it in other people’s code and eventually adopted it himself. The pattern is Receive an object, return an object (RORO).

Receive an object, return an object (RORO) Most of author’s functions now accept a single parameter of type object and many of them return or resolve to a value of type object as well. This patterns uses:

  • Named parameters
  • Cleaner default parameters
  • Richer return values
  • Easy function composition

RORO relies on destructuring feature introduced in ES2015. An added benefit of using destructuring for our parameter object is that it promotes immutability.

See function definition example bellow, with all parameters optional:

function findUsersByRole ({
  role,
  withContactInfo,
  includeInactive
} = {}) {...}

To learn more, read this article. Excellent!

[Read More]

Tags javascript programming nodejs