Skip to content

Conversation

@johndoknjas
Copy link
Contributor

Works towards finishing #16439, still a few hundred occurrences left. The ones I've changed here are some cases where it was unambiguous (at least from what I could tell) that type coercion couldn't be intended.

@ornicar
Copy link
Collaborator

ornicar commented Nov 19, 2024

Is === really better than ==?

I'm concerned this can create a number of subtle bugs that we'll be finding and fixing in the months to come.

@SergioGlorias
Copy link
Member

my main view on this:

  • == check the content
  • === checks content and type

I feel it may make a difference if the type of the variable received is different
I feel for the ts it doesn't make much difference since the typing strength would cause problems before

@johndoknjas
Copy link
Contributor Author

A key advantage of using === is that it avoids unexpected things like '1' == 1 or [] == '' being true due to type juggling. So === is closer in meaning than == to most popular languages' ==. In the lila codebase === is preferred, and I suspect most == are because people are used to it from other languages and accidentally type it out of habit. Granted there are rare exceptions where it's deliberate, such as the line under the comment // no strict equality here! in socket.ts.

I agree with Sergio that ts reduces the issue compared to js, but consider this scenario:

  • A function accepts two string params, and compares them with ==.
  • Later on, the code is updated so that one/both get the type number | string
  • Then the existing == code will give true for things like 1 == '1', even though that wasn't the intention.

For the changes in this PR, most fall under two categories:

  • Typescript told me that the two operands were of the same type. So changing == to === is guaranteed to have the same result in the current codebase, and it helps reduce unexpected bugs in the future.
  • One of the operands is a non-empty/non-zero literal. In these cases it's clear that the equality is only meant to be true when the other operand is the exact same.

@ornicar ornicar merged commit fdc1afc into lichess-org:master Nov 20, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

3 participants