<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bill Sourour</title>
    <description>The latest articles on DEV Community by Bill Sourour (@billsourour).</description>
    <link>https://dev.to/billsourour</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F63380%2F3f1b1f99-cb87-4501-b403-c8a4b83be67c.png</url>
      <title>DEV Community: Bill Sourour</title>
      <link>https://dev.to/billsourour</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/billsourour"/>
    <language>en</language>
    <item>
      <title>Elegant patterns in modern JavaScript: Ice Factory</title>
      <dc:creator>Bill Sourour</dc:creator>
      <pubDate>Sun, 06 May 2018 02:03:32 +0000</pubDate>
      <link>https://dev.to/billsourour/elegant-patterns-in-modern-javascript-icefactory-3k5h</link>
      <guid>https://dev.to/billsourour/elegant-patterns-in-modern-javascript-icefactory-3k5h</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1461023058943-07fcbe16d735%3Fixlib%3Drb-0.3.5%26ixid%3DeyJhcHBfaWQiOjEyMDd9%26s%3D89c21e12da5a1c16539ae341645fb184%26auto%3Dformat%26fit%3Dcrop%26w%3D2698%26q%3D80" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1461023058943-07fcbe16d735%3Fixlib%3Drb-0.3.5%26ixid%3DeyJhcHBfaWQiOjEyMDd9%26s%3D89c21e12da5a1c16539ae341645fb184%26auto%3Dformat%26fit%3Dcrop%26w%3D2698%26q%3D80"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Photo by &lt;a href="https://unsplash.com/photos/L-sm1B4L1Ns?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Demi DeHerrera&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/ice-coffee?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I’ve been working with JavaScript on and off since the late nineties. I didn’t really like it at first, but after the introduction of ES2015 (aka ES6), I began to appreciate JavaScript as an outstanding, dynamic programming language with enormous, expressive power.&lt;/p&gt;

&lt;p&gt;Over time, I’ve adopted several coding patterns that have lead to cleaner, more testable, more expressive code. Now, I am sharing these patterns with you.&lt;/p&gt;

&lt;p&gt;I wrote about the first pattern — “RORO” — &lt;a href="https://dev.to/billsourour/elegant-patterns-in-modern-javascript-roro-5b5i"&gt;here&lt;/a&gt;. Don’t worry if you haven’t read it, you can read these in any order.&lt;/p&gt;

&lt;p&gt;Today, I’d like to introduce you to the “Ice Factory” pattern.&lt;/p&gt;

&lt;p&gt;An Ice Factory is just &lt;strong&gt;a function that creates and returns a frozen object&lt;/strong&gt;. We’ll unpack that statement in a moment, but first let’s explore why this pattern is so powerful.&lt;/p&gt;
&lt;h3&gt;
  
  
  JavaScript classes are not so classy
&lt;/h3&gt;

&lt;p&gt;It often makes sense to group related functions into a single object. For example, in an e-commerce app, we might have a &lt;code&gt;cart&lt;/code&gt; object that exposes an &lt;code&gt;addProduct&lt;/code&gt; function and a &lt;code&gt;removeProduct&lt;/code&gt; function. We could then invoke these functions with &lt;code&gt;cart.addProduct()&lt;/code&gt; and &lt;code&gt;cart.removeProduct()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you come from a Class-centric, object oriented, programming language like Java or C#, this probably feels quite natural.&lt;/p&gt;

&lt;p&gt;If you’re new to programming — now that you’ve seen a statement like &lt;code&gt;cart.addProduct()&lt;/code&gt;. I suspect the idea of grouping together functions under a single object is looking pretty good.&lt;/p&gt;

&lt;p&gt;So how would we create this nice little &lt;code&gt;cart&lt;/code&gt; object? Your first instinct with modern JavaScript might be to use a &lt;code&gt;class&lt;/code&gt;. Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ShoppingCart.js

export default class ShoppingCart {
  constructor({db}) {
    this.db = db
  }

  addProduct (product) {
    this.db.push(product)
  }

  empty () {
    this.db = []
  }

get products () {
    return Object
      .freeze([...this.db])
  }

removeProduct (id) {
    // remove a product 
  }

// other methods

}

// someOtherModule.js

const db = [] 
const cart = new ShoppingCart({db})
cart.addProduct({ 
  name: 'foo', 
  price: 9.99
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;: I’m using an Array for the&lt;/em&gt; &lt;code&gt;db&lt;/code&gt; &lt;em&gt;parameter for simplicity’s sake. In real code this would be something like a&lt;/em&gt; &lt;a href="http://mongoosejs.com/docs/models.html" rel="noopener noreferrer"&gt;&lt;em&gt;Model&lt;/em&gt;&lt;/a&gt; &lt;em&gt;or&lt;/em&gt; &lt;a href="https://reallyshouldblogthis.blogspot.ca/2016/02/writing-pure-javascript-repository.html" rel="noopener noreferrer"&gt;&lt;em&gt;Repo&lt;/em&gt;&lt;/a&gt; &lt;em&gt;that interacts with an actual database.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Unfortunately — even though this looks nice — classes in JavaScript behave quite differently from what you might expect.&lt;/p&gt;

&lt;p&gt;JavaScript Classes will bite you if you’re not careful.&lt;/p&gt;

&lt;p&gt;For example, objects created using the &lt;code&gt;new&lt;/code&gt; keyword are mutable. So, you can actually &lt;strong&gt;re-assign&lt;/strong&gt; a method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const db = []
const cart = new ShoppingCart({db})

cart.addProduct = () =&amp;gt; 'nope!' 
// No Error on the line above!

cart.addProduct({ 
  name: 'foo', 
  price: 9.99
}) // output: "nope!" FTW?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even worse, objects created using the &lt;code&gt;new&lt;/code&gt; keyword inherit the &lt;code&gt;prototype&lt;/code&gt; of the &lt;code&gt;class&lt;/code&gt; that was used to create them. So, changes to a class’ &lt;code&gt;prototype&lt;/code&gt; affect &lt;strong&gt;all&lt;/strong&gt; objects created from that &lt;code&gt;class&lt;/code&gt; — even if a change is made &lt;strong&gt;after&lt;/strong&gt; the object was created!&lt;/p&gt;

&lt;p&gt;Look at this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cart = new ShoppingCart({db: []})
const other = new ShoppingCart({db: []})

ShoppingCart.prototype.addProduct = () =&amp;gt; ‘nope!’
// No Error on the line above!

cart.addProduct({ 
  name: 'foo', 
  price: 9.99
}) // output: "nope!"

other.addProduct({ 
  name: 'bar', 
  price: 8.88
}) // output: "nope!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then there's the fact that &lt;code&gt;this&lt;/code&gt; In JavaScript is dynamically bound. So, if we pass around the methods of our &lt;code&gt;cart&lt;/code&gt; object, we can lose the reference to &lt;code&gt;this&lt;/code&gt;. That’s very counter-intuitive and it can get us into a lot of trouble.&lt;/p&gt;

&lt;p&gt;A common trap is assigning an instance method to an event handler.&lt;/p&gt;

&lt;p&gt;Consider our &lt;code&gt;cart.empty&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;empty () {
    this.db = []
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we assign this method directly to the &lt;code&gt;click&lt;/code&gt; event of a button on our web page…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button id="empty"&amp;gt;
  Empty cart
&amp;lt;/button&amp;gt;

---

document
  .querySelector('#empty')
  .addEventListener(
    'click', 
    cart.empty
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;… when users click the empty &lt;code&gt;button&lt;/code&gt;, their &lt;code&gt;cart&lt;/code&gt; will remain full.&lt;/p&gt;

&lt;p&gt;It &lt;strong&gt;fails silently&lt;/strong&gt; because &lt;code&gt;this&lt;/code&gt; will now refer to the &lt;code&gt;button&lt;/code&gt; instead of the &lt;code&gt;cart&lt;/code&gt;. So, our &lt;code&gt;cart.empty&lt;/code&gt; method ends up assigning a new property to our &lt;code&gt;button&lt;/code&gt; called &lt;code&gt;db&lt;/code&gt; and setting that property to &lt;code&gt;[]&lt;/code&gt; instead of affecting the &lt;code&gt;cart&lt;/code&gt; object’s &lt;code&gt;db&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is the kind of bug that will drive you crazy because there is no error in the console and your common sense will tell you that it should work, but it doesn’t.&lt;/p&gt;

&lt;p&gt;To make it work we have to do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document
  .querySelector("#empty")
  .addEventListener(
    "click", 
    () =&amp;gt; cart.empty()
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document
  .querySelector("#empty")
  .addEventListener(
    "click", 
    cart.empty.bind(cart)
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think &lt;a href="https://medium.com/@mpjme" rel="noopener noreferrer"&gt;Mattias Petter Johansson&lt;/a&gt; &lt;a href="https://youtu.be/ImwrezYhw4w" rel="noopener noreferrer"&gt;said it best&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“&lt;/em&gt;&lt;code&gt;new&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;this&lt;/code&gt; &lt;em&gt;[in JavaScript] are some kind of unintuitive, weird, cloud rainbow trap.”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Ice Factory to the rescue
&lt;/h3&gt;

&lt;p&gt;As I said earlier, &lt;strong&gt;an Ice Factory is just a function that creates and returns a frozen object&lt;/strong&gt;. With an Ice Factory our shopping cart example looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// makeShoppingCart.js

export default function makeShoppingCart({
  db
}) {
  return Object.freeze({
    addProduct,
    empty,
    getProducts,
    removeProduct,
    // others
  })

function addProduct (product) {
    db.push(product)
  }

  function empty () {
    db = []
  }

function getProducts () {
    return Object
      .freeze(db)
  }

function removeProduct (id) {
    // remove a product
  }

// other functions
}

// someOtherModule.js

const db = []
const cart = makeShoppingCart({ db })
cart.addProduct({ 
  name: 'foo', 
  price: 9.99
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice our “weird, cloud rainbow traps” are gone:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;We no longer need&lt;/strong&gt; &lt;code&gt;new&lt;/code&gt; &lt;strong&gt;.&lt;/strong&gt;
We just invoke a plain old JavaScript function to create our &lt;code&gt;cart&lt;/code&gt; object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;We no longer need&lt;/strong&gt; &lt;code&gt;this&lt;/code&gt; &lt;strong&gt;.&lt;/strong&gt;
We can access the &lt;code&gt;db&lt;/code&gt; object directly from our member functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Our&lt;/strong&gt; &lt;code&gt;cart&lt;/code&gt; &lt;strong&gt;object is completely immutable.&lt;/strong&gt;
&lt;code&gt;Object.freeze()&lt;/code&gt; freezes the &lt;code&gt;cart&lt;/code&gt; object so that new properties can’t be added to it, existing properties can’t be removed or changed, and the prototype can’t be changed either. Just remember that &lt;code&gt;Object.freeze()&lt;/code&gt; is &lt;strong&gt;shallow&lt;/strong&gt; , so if the object we return contains an &lt;code&gt;array&lt;/code&gt; or another &lt;code&gt;object&lt;/code&gt; we must make sure to &lt;code&gt;Object.freeze()&lt;/code&gt; them as well. Also, if you’re using a frozen object outside of an &lt;a href="https://hacks.mozilla.org/2015/08/es6-in-depth-modules/" rel="noopener noreferrer"&gt;ES Module&lt;/a&gt;, you need to be in &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode" rel="noopener noreferrer"&gt;strict mode&lt;/a&gt; to make sure that re-assignments cause an error rather than just failing silently.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A little privacy please
&lt;/h3&gt;

&lt;p&gt;Another advantage of Ice Factories is that they can have private members. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function makeThing(spec) {
  const secret = 'shhh!'

return Object.freeze({
    doStuff
  })

function doStuff () {
    // We can use both spec
    // and secret in here 
  }
}

// secret is not accessible out here

const thing = makeThing()
thing.secret // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is made possible because of Closures in JavaScript, which you can read more about on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A little acknowledgement please
&lt;/h3&gt;

&lt;p&gt;Although Factory Functions have been around JavaScript forever, the Ice Factory pattern was heavily inspired by some code that &lt;a href="https://en.wikipedia.org/wiki/Douglas_Crockford" rel="noopener noreferrer"&gt;Douglas Crockford&lt;/a&gt; showed in &lt;a href="https://www.youtube.com/watch?v=rhV6hlL_wMc" rel="noopener noreferrer"&gt;this video&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here’s Crockford demonstrating object creation with a function he calls “constructor”:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vsbevgx03zge26wybdi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0vsbevgx03zge26wybdi.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Douglas Crockford demonstrating the code that inspired me.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;My Ice Factory version of the Crockford example above would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function makeSomething({ member }) {
  const { other } = makeSomethingElse() 

  return Object.freeze({ 
    other,
    method
  })

function method () {
    // code that uses "member"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I took advantage of function hoisting to put my return statement near the top, so that readers would have a nice little summary of what’s going on before diving into the details.&lt;/p&gt;

&lt;p&gt;I also used destructuring on the &lt;code&gt;spec&lt;/code&gt; parameter. And I renamed the pattern to “Ice Factory” so that it’s more memorable and less easily confused with the &lt;code&gt;constructor&lt;/code&gt; function from a JavaScript &lt;code&gt;class&lt;/code&gt;. But it’s basically the same thing.&lt;/p&gt;

&lt;p&gt;So, credit where credit is due, thank you Mr. Crockford.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;It’s probably worth mentioning that Crockford considers function “hoisting” a “bad part” of JavaScript and would likely consider my version heresy. I discussed my feelings on this in a&lt;/em&gt; &lt;a href="https://medium.freecodecamp.org/constant-confusion-why-i-still-use-javascript-function-statements-984ece0b72fd" rel="noopener noreferrer"&gt;&lt;em&gt;previous article&lt;/em&gt;&lt;/a&gt; &lt;em&gt;and more specifically,&lt;/em&gt; &lt;a href="https://medium.com/@BillSourour/thank-you-for-your-thoughtful-response-7542ba5ff94e" rel="noopener noreferrer"&gt;&lt;em&gt;this comment&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What about inheritance?
&lt;/h3&gt;

&lt;p&gt;If we tick along building out our little e-commerce app, we might soon realize that the concept of adding and removing products keeps cropping up again and again all over the place.&lt;/p&gt;

&lt;p&gt;Along with our Shopping Cart, we probably have a Catalog object and an Order object. And all of these probably expose some version of &lt;code&gt;addProduct&lt;/code&gt; and &lt;code&gt;removeProduct&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We know that duplication is bad, so we’ll eventually be tempted to create something like a Product List object that our cart, catalog, and order can all inherit from.&lt;/p&gt;

&lt;p&gt;But rather than extending our objects by inheriting a Product List, we can instead adopt the timeless principle offered in one of the most influential programming books ever written:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Favor object composition over class inheritance.” &lt;br&gt;&lt;br&gt;
– Design Patterns: Elements of Reusable Object-Oriented Software.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In fact, the authors of that book — colloquially known as “The Gang of Four” — go on to say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“…our experience is that designers overuse inheritance as a reuse technique, and designs are often made more reusable (and simpler) by depending more on object composition.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, here’s our product list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function makeProductList({ productDb }) {
  return Object.freeze({
    addProduct,
    empty,
    getProducts,
    removeProduct,
    // others
  )}
  // definitions for 
  // addProduct, etc...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here’s our shopping cart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function makeShoppingCart(productList) {

return Object.freeze({
      items: productList,
      someCartSpecificMethod,
      // ...
    )}

function someCartSpecificMethod () {
    // code 
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we can just inject our Product List into our Shopping Cart, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const productDb = []
const productList = makeProductList({ productDb })

const cart = makeShoppingCart(productList)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And use the Product List via the &lt;code&gt;items&lt;/code&gt; property. Like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cart.items.addProduct()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It may be tempting to subsume the entire Product List by incorporating its methods directly into the shopping cart object, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function makeShoppingCart({ 
   addProduct,
   empty,
   getProducts,
   removeProduct,
   ...others
  }) {

return Object.freeze({
      addProduct,
      empty,
      getProducts,
      removeProduct,
      someOtherMethod,
      ...others
    )}

function someOtherMethod () {
    // code 
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In fact, in an earlier version of this article, I did just that. But then it was pointed out to me that this is a bit dangerous (as explained &lt;a href="https://www.reddit.com/r/programming/comments/5dxq6i/composition_over_inheritance/da8bplv/" rel="noopener noreferrer"&gt;here&lt;/a&gt;). So, we're better off sticking with proper object composition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Awesome. I’m Sold!
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnp8s4ywy7buykonj1sb2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnp8s4ywy7buykonj1sb2.jpeg"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Careful&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whenever we’re learning something new, especially something as complex as software architecture and design, we tend to want hard and fast rules. We want to hear thing like “&lt;em&gt;always&lt;/em&gt; do this” and “ &lt;em&gt;never&lt;/em&gt; do that.”&lt;/p&gt;

&lt;p&gt;The longer I spend working with this stuff, the more I realize that there’s no such thing as &lt;em&gt;always&lt;/em&gt; and &lt;em&gt;never.&lt;/em&gt; It’s about choices and trade-offs.&lt;/p&gt;

&lt;p&gt;Making objects with an Ice Factory is slower and takes up more memory than using a class.&lt;/p&gt;

&lt;p&gt;In the types of use case I’ve described, this won’t matter. Even though they are slower than classes, Ice Factories are still quite fast.&lt;/p&gt;

&lt;p&gt;If you find yourself needing to create hundreds of thousands of objects in one shot, or if you’re in a situation where memory and processing power is at an extreme premium you might need a class instead.&lt;/p&gt;

&lt;p&gt;Just remember, profile your app first and don’t prematurely optimize. Most of the time, object creation is not going to be the bottleneck.&lt;/p&gt;

&lt;p&gt;Despite my earlier rant, Classes are not always terrible. You shouldn’t throw out a framework or library just because it uses classes. In fact, &lt;a href="https://medium.com/@dan_abramov" rel="noopener noreferrer"&gt;Dan Abramov&lt;/a&gt; wrote pretty eloquently about this in his article, &lt;a href="https://medium.com/@dan_abramov/how-to-use-classes-and-sleep-at-night-9af8de78ccb4" rel="noopener noreferrer"&gt;How to use Classes and Sleep at Night&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, I need to acknowledge that I’ve made a bunch of opinionated style choices in the code samples I’ve presented to you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://medium.freecodecamp.org/constant-confusion-why-i-still-use-javascript-function-statements-984ece0b72fd" rel="noopener noreferrer"&gt;I use function statements instead of function expressions&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;I put my return statement near the top (this is made possible by my use of function statements, see above).&lt;/li&gt;
&lt;li&gt;I name my factory function, &lt;code&gt;makeX&lt;/code&gt; instead of &lt;code&gt;createX&lt;/code&gt; or &lt;code&gt;buildX&lt;/code&gt; or something else.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://medium.freecodecamp.org/elegant-patterns-in-modern-javascript-roro-be01e7669cbd" rel="noopener noreferrer"&gt;My factory function takes a single, destructured, parameter object&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;I don’t use semi-colons (&lt;a href="https://github.com/twbs/bootstrap/issues/3057" rel="noopener noreferrer"&gt;Crockford would also NOT approve of that&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;and so on…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You may make different style choices, and &lt;a href="https://medium.freecodecamp.org/the-100-correct-coding-style-guide-5b594a1655f0" rel="noopener noreferrer"&gt;that’s okay&lt;/a&gt;! The style is not the pattern.&lt;/p&gt;

&lt;p&gt;The Ice Factory pattern is just: &lt;strong&gt;use a function to create and return a frozen object&lt;/strong&gt;. Exactly how you write that function is up to you.&lt;/p&gt;




&lt;p&gt;If you’ve found this article useful help me spread the word with some hearts and unicorns! And if you'd like to learn more stuff like this, please sign up for my Dev Mastery newsletter below. Thanks!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="http://www.devmastery.com" rel="noopener noreferrer"&gt;Dev Mastery Newsletter Sign Up&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;I keep your info private and I never SPAM.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>patterns</category>
      <category>architecture</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Elegant patterns in modern JavaScript: RORO</title>
      <dc:creator>Bill Sourour</dc:creator>
      <pubDate>Tue, 20 Mar 2018 19:51:51 +0000</pubDate>
      <link>https://dev.to/billsourour/elegant-patterns-in-modern-javascript-roro-5b5i</link>
      <guid>https://dev.to/billsourour/elegant-patterns-in-modern-javascript-roro-5b5i</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5ui7h5hll0p7z57474vd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F5ui7h5hll0p7z57474vd.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wrote my first few lines of JavaScript not long after the language was invented. If you told me at the time that I would one day be writing &lt;a href="https://medium.com/search?q=Elegant%20patterns%20in%20JavaScript" rel="noopener noreferrer"&gt;a series of articles&lt;/a&gt; about &lt;strong&gt;elegant&lt;/strong&gt; patterns in JavaScript, I would have laughed you out of the room. I thought of JavaScript as a strange little language that barely even qualified as “real programming.”&lt;/p&gt;

&lt;p&gt;Well, a lot has changed in the 20 years since then. I now see in JavaScript what &lt;a href="https://en.wikipedia.org/wiki/Douglas_Crockford" rel="noopener noreferrer"&gt;Douglas Crockford&lt;/a&gt; saw when he wrote &lt;em&gt;JavaScript: The Good Parts&lt;/em&gt;: “An outstanding, dynamic programming language … with enormous, expressive power.”&lt;/p&gt;

&lt;p&gt;So, without further ado, here is a wonderful little pattern I’ve been using in my code lately. I hope you come to enjoy it as much as I have.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Please note&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;: I’m pretty sure I did not invent any of this. Chances are I came across it in other people’s code and eventually adopted it myself.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Receive an object, return an object (RORO)
&lt;/h2&gt;

&lt;p&gt;Most of my functions now accept a single parameter of type &lt;code&gt;object&lt;/code&gt; and many of them return or resolve to a value of type &lt;code&gt;object&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;Thanks in part to the &lt;em&gt;destructuring&lt;/em&gt; feature introduced in ES2015, I’ve found this to be a powerful pattern. I’ve even given it the silly name, “RORO” because… branding? 🤷‍♂️&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;Destructuring is one of my favorite features of modern JavaScript. We’re going to be taking advantage of it quite a bit throughout this article, so if you’re not familiar with it, here’s a quick video from &lt;a href="https://www.youtube.com/playlist?list=PLWKjhJtqVAbk2qRZtWSzCIN38JC_NdhW5" rel="noopener noreferrer"&gt;Beau Carnes&lt;/a&gt; to get you up to speed.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/-vR3a11Wzt0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here are some reasons why you’ll love this pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Named parameters&lt;/li&gt;
&lt;li&gt;Cleaner default parameters&lt;/li&gt;
&lt;li&gt;Richer return values&lt;/li&gt;
&lt;li&gt;Easy function composition&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s look at each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Named Parameters
&lt;/h2&gt;

&lt;p&gt;Suppose we have a function that returns a list of Users in a given Role and suppose we need to provide an option for including each User’s Contact Info and another option for including Inactive Users, traditionally we might write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A call to this function might then look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how ambiguous those last two parameters are. What does “true, true” refer to?&lt;/p&gt;

&lt;p&gt;What happens if our app almost never needs Contact Info but almost always needs Inactive Users? We have to contend with that middle parameter all the time, even though it’s not really relevant (more on that later).&lt;/p&gt;

&lt;p&gt;In short, this traditional approach leaves us with potentially ambiguous, noisy code that’s harder to understand and trickier to write.&lt;/p&gt;

&lt;p&gt;Let’s see what happens when we receive a single object instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice our function looks almost identical except that &lt;strong&gt;we’ve put braces around our parameters&lt;/strong&gt;. This indicates that instead of receiving three distinct parameters, our function now expects a single object with properties named &lt;code&gt;role&lt;/code&gt;, &lt;code&gt;withContactInfo&lt;/code&gt;, and &lt;code&gt;includeInactive&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This works because of a JavaScript feature introduced in ES2015 called &lt;em&gt;Destructuring&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now we can call our function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;includeInactive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is far less ambiguous and a lot easier to read and understand. Plus, omitting or re-ordering our parameters is no longer an issue since they are now the named properties of an object.&lt;/p&gt;

&lt;p&gt;For example, this works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;includeInactive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And so does this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;includeInactive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This also makes it possible to add new parameters without breaking old code.&lt;/p&gt;

&lt;p&gt;One important note here is that if we want all the parameters to be optional, in other words, if the following is a valid call…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;… we need to set a default value for our parameter object, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An added benefit of using destructuring for our parameter object is that it promotes immutability. When we destructure the &lt;code&gt;object&lt;/code&gt; on its way into our function we assign the object’s properties to new variables. Changing the value of those variables will not alter the original object.&lt;/p&gt;

&lt;p&gt;Consider the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;includeInactive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'admin'&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'Admin'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though we change the value of &lt;code&gt;role&lt;/code&gt; the value of &lt;code&gt;options.role&lt;/code&gt; remains unchanged.&lt;/p&gt;

&lt;p&gt;It’s worth noting that destructuring makes a_ &lt;strong&gt;&lt;em&gt;shallow&lt;/em&gt;&lt;/strong&gt; _copy so if any of the properties of our parameter object are of a complex type (e.g. &lt;code&gt;array&lt;/code&gt; or &lt;code&gt;object&lt;/code&gt;) changing them would indeed affect the original.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cleaner Default Parameters
&lt;/h2&gt;

&lt;p&gt;With ES2015 JavaScript functions gained the ability to define default parameters. In fact, we recently used a default parameter when we added &lt;code&gt;={}&lt;/code&gt; to the parameter object on our &lt;code&gt;findUsersByRole&lt;/code&gt; function above.&lt;/p&gt;

&lt;p&gt;With traditional default parameters, our &lt;code&gt;findUsersByRole&lt;/code&gt; function might look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to set &lt;code&gt;includeInactive&lt;/code&gt; to &lt;code&gt;true&lt;/code&gt; we have to explicitly pass &lt;code&gt;undefined&lt;/code&gt; as the value for &lt;code&gt;withContactInfo&lt;/code&gt; to preserve the default, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Admin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How hideous is that?&lt;/p&gt;

&lt;p&gt;Compare it to using a parameter object like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can write…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;findUsersByRole&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;Admin&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;includeInactive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;… and our default value for &lt;code&gt;withContactInfo&lt;/code&gt; is preserved.&lt;/p&gt;

&lt;h2&gt;
  
  
  BONUS: Required Parameters
&lt;/h2&gt;

&lt;p&gt;How often have you written something like this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;We use double equals (==) above to test for both&lt;/em&gt; &lt;code&gt;null&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;undefined&lt;/code&gt; &lt;em&gt;with a single statement.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;What if I told you that you could use default parameters to validate required parameters instead?&lt;/p&gt;

&lt;p&gt;First, we need to define a &lt;code&gt;requiredParam()&lt;/code&gt; function that throws an Error.&lt;/p&gt;

&lt;p&gt;Like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;requiredParam&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requiredParamError&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="s2"&gt;`Required parameter, "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;param&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;" is missing.`&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// preserve original stack trace&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;captureStackTrace&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;captureStackTrace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;requiredParamError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;requiredParam&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;requiredParamError&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;em&gt;I know, I know. requiredParam doesn’t RORO. That’s why I said&lt;/em&gt; &lt;strong&gt;&lt;em&gt;many&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;of my functions — not&lt;/em&gt; &lt;strong&gt;&lt;em&gt;all&lt;/em&gt;&lt;/strong&gt; &lt;em&gt;.&lt;/em&gt;
&lt;/h4&gt;

&lt;p&gt;Now, we can set an invocation of &lt;code&gt;requiredParam&lt;/code&gt; as the default value for &lt;code&gt;role&lt;/code&gt;, like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;findUsersByRole&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;requiredParam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;role&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="nx"&gt;withContactInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;includeInactive&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{})&lt;/span&gt; &lt;span class="p"&gt;{...}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the above code, if anyone calls &lt;code&gt;findUsersByRole&lt;/code&gt; without supplying a &lt;code&gt;role&lt;/code&gt; they will get an &lt;code&gt;Error&lt;/code&gt; that says &lt;code&gt;Required parameter, “role” is missing.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Technically, we can use this technique with regular default parameters as well; we don’t necessarily need an object. But this trick was too useful not to mention.&lt;/p&gt;

&lt;h2&gt;
  
  
  Richer Return Values
&lt;/h2&gt;

&lt;p&gt;JavaScript functions can only return a single value. If that value is an &lt;code&gt;object&lt;/code&gt; it can contain a lot more information.&lt;/p&gt;

&lt;p&gt;Consider a function that saves a &lt;code&gt;User&lt;/code&gt; to a database. When that function returns an object it can provide a lot of information to the caller.&lt;/p&gt;

&lt;p&gt;For example, a common pattern is to “upsert” or “merge” data in a save function. Which means, we insert rows into a database table (if they do not already exist) or update them (if they do exist).&lt;/p&gt;

&lt;p&gt;In such cases, it would be handy to know wether the operation performed by our Save function was an &lt;code&gt;INSERT&lt;/code&gt; or an &lt;code&gt;UPDATE&lt;/code&gt;. It would also be good to get an accurate representation of exactly what was stored in the database, and it would be good to know the status of the operation; did it succeed, is it pending as part of a larger transaction, did it timeout?&lt;/p&gt;

&lt;p&gt;When returning an object, it’s easy to communicate all of this info at once.&lt;/p&gt;

&lt;p&gt;Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;upsert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// save to the DB&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// e.g 'INSERT'&lt;/span&gt;
    &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// e.g. 'Success'&lt;/span&gt;
    &lt;span class="na"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;userInfo&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Technically, the above returns a &lt;code&gt;Promise&lt;/code&gt; that resolves to an &lt;code&gt;object&lt;/code&gt; but you get the idea.&lt;/p&gt;

&lt;h2&gt;
  
  
  Easy Function Composition
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;“Function composition is the process of combining two or more functions to produce a new function. Composing functions together is like snapping together a series of pipes for our data to flow through.” —  Eric Elliott&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can compose functions together using a &lt;code&gt;pipe&lt;/code&gt; function that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;param&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;param&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above function takes a list of functions and returns a function that can apply the list from left to right, starting with a given parameter and then passing the result of each function in the list to the next function in the list.&lt;/p&gt;

&lt;p&gt;Don’t worry if you’re confused, there’s an example below that should clear things up.&lt;/p&gt;

&lt;p&gt;One limitation of this approach is that each function in the list must only receive a single parameter. Luckily, when we RORO that’s not a problem!&lt;/p&gt;

&lt;p&gt;Here’s an example where we have a &lt;code&gt;saveUser&lt;/code&gt; function that pipes a &lt;code&gt;userInfo&lt;/code&gt; object through 3 separate functions that validate, normalize, and persist the user information in sequence.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;persist&lt;/span&gt;
  &lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;userInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters" rel="noopener noreferrer"&gt;rest parameter&lt;/a&gt; in our &lt;code&gt;validate&lt;/code&gt;, &lt;code&gt;normalize&lt;/code&gt;, and &lt;code&gt;persist&lt;/code&gt; functions to destructure only the values that each function needs and still pass everything back to the caller.&lt;/p&gt;

&lt;p&gt;Here’s a bit of code to give you the gist of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;requiredParam&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;requiredParam&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nx"&gt;pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;requiredParam&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// do some validation&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;normalize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// do some normalizing&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;persist&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="nx"&gt;upsert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// save userInfo to the DB&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;operation&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;saved&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;info&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  To RO or not to RO, that is the question
&lt;/h2&gt;

&lt;p&gt;I said at the outset, &lt;strong&gt;most&lt;/strong&gt; of my functions receive an object and &lt;strong&gt;many&lt;/strong&gt; of them return an object too.&lt;/p&gt;

&lt;p&gt;Like any pattern, RORO should be seen as just another tool in our tool box. We use it in places where it adds value by making a list of parameters more clear and flexible and by making a return value more expressive.&lt;/p&gt;

&lt;p&gt;If you’re writing a function that will only ever need to receive a single parameter, then receiving an &lt;code&gt;object&lt;/code&gt; is overkill. Likewise, if you’re writing a function that can communicate a clear and intuitive response to the caller by returning a simple value, there is no need to return an &lt;code&gt;object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An example where I almost never RORO is assertion functions. Suppose we have a function &lt;code&gt;isPositiveInteger&lt;/code&gt; that checks wether or not a given parameter is a positive integer, such a function likely wouldn’t benefit from RORO at all.&lt;/p&gt;




&lt;p&gt;If you enjoyed this article, please tap the ♥️ icon to help spread the word. And if you want to read more stuff like this, please sign up for my Dev Mastery newsletter below.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;a href="https://upscri.be/83d379/" rel="noopener noreferrer"&gt;Sign up to the DevMastery Newsletter&lt;/a&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;I keep your info private and I NEVER spam.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>software</category>
      <category>javascript</category>
      <category>patterns</category>
    </item>
  </channel>
</rss>
