mercredi 13 décembre 2017

How to share logic among functions?

I came across with an issue when developing a code with some similar blocks inside my code. My question is: What is the best approach to share logic among functions?

Example:

The functions below contains the same if/else logic. How can we refactor this code in order to have a more concise and maintainable code?

// pseudo code...

const hirer = 'woman';

const getPositions = () => {
 if (hirer === 'woman') {
   getPositionsFromWomen();
   // do other stufs here...
 } else if (hirer === 'man') {
   getPositionFromMen();
   // do other stufs here...
 }
 // maybe other stufs here...
}


const hire = (hirer) => {
  if (hirer === 'woman') {
    increaseWomenHiringRate(hirer);
    // do other stufs here...
  } else if (hirer === 'man') {
    increaseMenHiringRate(hirer);
    // do other stufs here...
  }
 setPositionClosed();
}

Aucun commentaire:

Enregistrer un commentaire