Express.js handler to update user fields I have an express handler which i thought was pretty simple, but CodeClimate flagged this method as having a Cognitive complexity of 6 (5 is the max by default without flagging something)

Curious how I could simplify this method to reduce Cognitive complexity.

function updateUser (req, res) {
  // Update any fields that were passed in.
  // Explicitly checking for undefined b/c passing null values should set them to null in the db
  if (req.body.firstName !== undefined) {
    req.user.firstName = req.body.firstName
  }

  if (req.body.lastName !== undefined) {
    req.user.lastName = req.body.lastName
  }

  if (req.body.email !== undefined) {
    req.user.email = req.body.email
  }

  if (req.body.phone !== undefined) {
    req.user.phone = req.body.phone
  }

  if (req.body.fax !== undefined) {
    req.user.fax = req.body.fax
  }

  if (req.body.notes !== undefined) {
    req.user.notes = req.body.notes
  }

  req.user.save()
    .then(user =&gt; {
      return res.status(HttpStatus.OK).send(user)
    })
    .catch(err =&gt; {
      req.log.error(err)
      return handleErr(res, HttpStatus.INTERNAL_SERVER_ERROR, err.message)
    })
}
Assignments can be contracted to something like below:

 req.user.firstName = req.body.firstName || req.user.firstName;
 req.user.lastName = req.body.lastName || req.user.lastName;
 req.user.email = req.body.email || req.user.email;
 req.user.phone = req.body.phone || req.user.phone;
 req.user.fax = req.body.fax || req.user.fax;
 req.user.notes = req.body.notes || req.user.notes;


I think it can further be reduced if properties are put in some array and iterated over (since source and destination property have the same name), but I would stick with this arguably more readable text.

I am not familiar with express.js, so the following might not be applicable (I usually work with Typescript in Angular).

Your request have both a body and some user property. It is recommended to clearly separate output from input in order to obtain pure functions as much as possible. Something like below:

 updateUserFromRequest(req, user) {

     // clone to avoid changing provided reference
     var ret = [... user];

     // assignment logic here using ret

     return ret;
 }