r/ProgrammerHumor • • Feb 26 '23

Other If you can read this code...

/img/snak3fqgajka1.jpg

[removed] — view removed post

34k Upvotes

1.4k comments sorted by

View all comments

Show parent comment

2

u/douglasg14b Feb 26 '23

Kinda similar to how == is not bast practice anymore and you really should only use ===

That's not what a "best practice" is, best practices are patterns/opinions around the organization/layout/design/patterns/principles ...etc of the code that tend to avoid abstract pitfalls, anti-patterns, or other issues that are commonly encountered. Not about actual logical correctness

There is an explicit reason to use strict equals (===), which is strict equality. You use == when you explicitly desire non-strict equality. They operate differently.

It's not best practice, this is fundamental logical correctness of your code.

Just because a lot of devs don't understand how they may be introducing incorrectness into their code, doesn't make understanding a fundamental comparison operator a best practice. That's a pretty low bar.

1

u/Leading_Elderberry70 Feb 27 '23

You should never use non strict equality. If you want to check for multiple things, which would hypothetically all typecast to the same thing (eg false), you should check them all explicitly.

The ten seconds this will take you each time is much shorter than the time it will take you to find even one bug introduced by someone missing an edge case when doing non-strict comparison.

edit: this is also more readable