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

Negative boolean variable names #63

Closed
jltwheeler opened this issue May 20, 2021 · 1 comment
Closed

Negative boolean variable names #63

jltwheeler opened this issue May 20, 2021 · 1 comment

Comments

@jltwheeler
Copy link

jltwheeler commented May 20, 2021

Suggestion to explain the best approach for naming negative booleans.

Example: if you create a variable for checking if an object is a dog , you could either do:

const isDog = (dog) => dog.sound() === 'bark'

if (!isDog(pet)) {
    // handle the not dog logic
}

or

const isNotDog = (dog) => dog.sound() !== 'bark'

if (isNotDog(pet)) {
    // handle the not dog logic
}
@kettanaito
Copy link
Owner

Thanks for the suggestion, @jltwheeler.

I'd suggest going with a positive isDog and negating it when necessary.

const isDog = (dog) => dog.sound() === 'bark'

if (isDog(pet)) {
  // handle a situation when pet is a dog
}

if (!isDog(pet)) {
  // handle a situation when pet isn't a dog
}

I rarely encounter a need for negative boolean variables, so please feel free to reference any examples you see fit.

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