fac25 / amai-mtoto

amai-mtoto.vercel.app
1 stars 1 forks source link

more explicit conditional code #110

Open jackherizsmith opened 1 year ago

jackherizsmith commented 1 year ago

so this is just an opinion: I found this function in /firebase/firestore.js which uses unconventional Boolean logic to run some code or not, whereas typically you would see that pattern in JSX.

It's this:

async function updateBabySize(weekNum, { description, imgSrc, alt, weight }) {
  const babySizeRef = doc(db, "babySize", weekNum);
  description && (await updateDoc(babySizeRef, { description }));
  imgSrc && (await updateDoc(babySizeRef, { imgSrc }));
  alt && (await updateDoc(babySizeRef, { alt }));
  weight && (await updateDoc(babySizeRef, { weight }));
}

this is obviously working just fine so no need to change it - however, I think it's more readable / easier to understand with a more common pattern, i.e.

if (description) {
  await updateDoc(babySizeRef, { description });
}

(or, if you like doing things in one line)

if (description) await updateDoc(babySizeRef, { description });
snim2 commented 1 year ago

+1 In general complex Boolean expressions are especially hard for humans to read, and tend to be a common source of bugs, so anything you can do to simplify them, or simplify your control-flow in general is a win.