-
Notifications
You must be signed in to change notification settings - Fork 4
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
Allow loose equal for null comparison #72
Comments
I agree, checking for However, there are three reasons why I'm hesitant to add this:
Is there a popular library that mixes both What do you think? :) |
Good point! We currently run into that case with the graphql-codegen. Here is an excerpt of the code on the landing page of the project: Schema type Query {
me: User!
user(id: ID!): User
allUsers: [User]
search(term: String!): [SearchResult!]!
myChats: [Chat!]!
} Generated output export type Query = {
__typename?: 'Query',
me: User,
user?: Maybe<User>,
allUsers?: Maybe<Array<Maybe<User>>>,
search: Array<SearchResult>,
myChats: Array<Chat>,
};
I did not know that. That sucks big time :/. So we might still be better of with a helper function that checks for both
I think you are right with this, especially since it is hard to spot the missing equal, if you are used to see tripple equals everywhere else. But once you get used to reading it, it can improve readability since you do not need to mentally skip every second line. Here is an example from a recent code review: if (
creditcardNumber != null &&
creditCardAccountholder != null &&
expirationDate != null
) { … }
// vs
if (
creditcardNumber !== null &&
creditcardNumber !== undefined &&
creditCardAccountholder !== null &&
creditCardAccountholder !== undefined &&
expirationDate !== null
expirationDate !== undefined
) { … }
I am not so sure about this anymore. The inability to optimize it properly and the learning curve for beginners throw me off. For both cases it might be superior to have a helper function to deal with this stuff. I’ll close the issue for now. |
In an effort to make these rules less annoying for most people I decided to allow this kind of pattern (although I personally dislike it :)) |
It's quite common for developers to use loose comparison to compare against null. Although this pattern is perceived as problematic by some developers, it can be quite useful in other situations. The validity of this comparison should be checked during code review. See #72
Checking for
null
orundefined
can be really verbose. Especially if we have some optional chaining. A recent example:with optional chaining this becomes easier:
but it is still pretty verbose. I am not in favor of allowing losse equality everywhere, but since the
eqeqeq
rule has a smart rule for exactly that case I would like to discuss it here.The example from above would look like this then:
What do you think?
The text was updated successfully, but these errors were encountered: