tc39 / proposal-negated-in-instanceof

A proposal to introduce negated in and instanceof operators to JavaScript
https://tc39.es/proposal-negated-in-instanceof/
MIT License
53 stars 0 forks source link

Comparison of prefix vs infix negation #8

Open gorosgobe opened 10 months ago

gorosgobe commented 10 months ago

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 commented 10 months ago

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 commented 5 months ago

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 commented 5 months ago

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