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

Comparison of prefix vs infix negation #8

Open
gorosgobe opened this issue Nov 13, 2023 · 3 comments
Open

Comparison of prefix vs infix negation #8

gorosgobe opened this issue Nov 13, 2023 · 3 comments

Comments

@gorosgobe
Copy link
Collaborator

gorosgobe commented Nov 13, 2023

During the Tokyo TC39 meeting, some committee members mentioned that they were skeptical that introducing a new way to negate in and instanceof expressions would help reduce the number of bugs in these expressions.

I collected some data through Sourcegraph to see the popularity of infix negation (this proposal) versus prefix negation (current way of negating in and instanceof) in Kotlin and Python, which are the most popular languages using either of the syntax options (! and not). If infix negation is more popular, then this would suggest that introducing infix negation through negated in and instanceof operators would indeed be a more popular option, and over time the number of bugs would reduce.

Language Operator Prefix negation Infix negation Ratio Queries
Python not a in b
a not in b
178k 791k 4.4x not a in b
a not in b
Python not a is C
a is not C
11k 1.9M 173x not a is C
a is not C
Kotlin !(a in b)
a !in b
23 21k 913x !(a in b)
a !in b
Kotlin !(a is C)
a !is C
371 53k 143x !(a is C)
a !is C

Note: In Python, not has lower precedence than in and is, so no parentheses are needed. In Kotlin, in contrast, ! has higher precedence, so parentheses are required.

Overall, the data shows that infix negation is substantially more popular than prefix negation across both languages, with Kotlin showing the biggest difference between prefix and infix forms.

@DanielRosenwasser
Copy link
Member

Thanks for putting this together! I highly suspect that the occurrence of prefix notation is so much lower in Kotlin is because of the de facto use of IDE tooling with refactoring suggestions. It's likely that Kotlin devs get a nudge to write in the more canonical form.

It's admittedly a little bit harder to do that with a language like JavaScript because anecdotally users don't always like being nudged into something that feels stylistic. That's often left for formatters and linters, but it's still a possibility.

@Fayti1703
Copy link

Overall, the data shows that prefix negation is substantially more popular than infix negation across both languages, with Kotlin showing the biggest difference between prefix and infix forms.

This conclusion seems to be backwards? Based on the data in the table, it should read:

Overall, the data shows that infix negation is substantially more popular than prefix negation across both languages, with Kotlin showing the biggest difference between prefix and infix forms.

@gorosgobe
Copy link
Collaborator Author

@Fayti1703 thanks for catching this, it has now been corrected!

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

3 participants