0

I'm writing some code to decide whether it needs to use mock data. Here is the code:

let useMocks = false

const setMockSwitch = ({idNumber}) => {
    if (idNumber && idNumber === mockUser.idNumber) {
        useMocks = true
    }

    if (idNumber && idNumber !== mockUser.idNumber) {
        useMocks = false
    }
}

in case idNumber is passed to the function and it equals to a mockUser than "toggle" the switch...

I'm trying to think about logical solution how to shorten the code and make it more readable. I'll appreciate your help! Thanks!

Pavel T
  • 79
  • 5

3 Answers3

4

Well you could just take advantage of the fact that comparisons produce boolean values:

const setMockSwitch = ({idNumber}) => {
  if (idNumber)
    useMocks = idNumber === mockUser.idNumber;
};

Oh and note that if mockUser.idNumber can ever be 0, this will have problems (as would the original code) because 0 tests as false in code like this.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    This would generate a bug if the value of `idNumber` passed `0`. Because `!!0` would return `false`. It's better to use this code instead `(typeof idNumber !== 'undefined' && idNumber === mockUser.idNumber)` – Spleen Jun 16 '19 at 12:12
  • 1
    I don't need to set the useMock variable every time the function being called, it needs to be set to true only when idNumber exists and equals to mockUser.idNumber. The same goes for the second if statement. Most of the time the function being called idNumber === undefined. – Pavel T Jun 16 '19 at 12:13
  • @SajadJafari yes, that's noted in the answer. The original code would also have that problem; it's not clear whether the original code is successful in actual use. – Pointy Jun 16 '19 at 12:13
  • @PavelT ah, good point; give me a second — OK answer updated; I'm pretty sure that it will behave like your original now. – Pointy Jun 16 '19 at 12:14
0

Assuming mockUser.idNumber has the actual value you are looking for, you could just do,

const setMockSwitch = ({idNumber}) => {
     useMocks = idNumber === mockUser.idNumber;
}
nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

You can reduce this to a single line arrow function. By leveraging the short-circuit && operator you can make sure useMocks is set only if idNumber is defined

let useMocks = false

const setMockSwitch = ({idNumber}) => idNumber && (useMocks = idNumber === mockUser.idNumber)

You can also write it to be side-effect free, like so,

const shouldUseMocks = ({idNumber}) => idNumber === mockUser.idNumber

idNumber && useMocks = shouldUseMocks({idNumber})

This allows you to extract this method into a library of helpers, and re-use it, as it does not directly modify variables from the outer scope. This would be my preferred and recommended way.

If you want to account for 0 being a valid idNumber,

let useMocks = false

const setMockSwitch = ({idNumber}) => idNumber !== void 0 && (useMocks = idNumber === mockUser.idNumber)

void 0 always returns the true primitive undefined and not any local variables called undefined, therefore is the cleanest way to check for undefined. In fact you can place the void operator in front of any other expression to force it to return undefined

Avin Kavish
  • 8,317
  • 1
  • 21
  • 36