Home

Default values = better code

This is a short illustration why using default values can improve your code

//version 1

const { pending_actions } = orchestration_pathway_reference

if (!Array.isArray(pending_actions) ||
    (Array.isArray(pending_actions) && pending_actions.length === 0)
) {
  return 'Nothing to report'
}

You can do

 //version 2
  
const { pending_actions = [] } = orchestration_pathway_reference

if (pending_actions.length === 0) {
  return 'Nothing to report'
}

What is wrong with version 1?

  • the code implies that pending_actions could be something else than an array
  • has unnecessary code (i.e. !Array.isArray(pending_actions) ||
    (Array.isArray(pending_actions))