Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more explicit conditional code #110

Open
jackherizsmith opened this issue Dec 5, 2022 · 1 comment
Open

more explicit conditional code #110

jackherizsmith opened this issue Dec 5, 2022 · 1 comment

Comments

@jackherizsmith
Copy link

jackherizsmith commented Dec 5, 2022

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
Copy link

snim2 commented Dec 6, 2022

+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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants