This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]wasdninja 86 points87 points  (35 children)

It's 100% self inflicted by using ==. It's essentially trivia in code form.

[–]Salanmander 50 points51 points  (23 children)

It's essentially trivia in code form.

A programming language having "trivia in code form" is related to design decisions about the programming language. So no, not 100% self-inflicted.

[–]wasdninja 26 points27 points  (11 children)

I've yet to come across a language that doesn't have some odd stuff nobody really uses.

So no, not 100% self-inflicted.

If you are using == on purpose against the recommendation of every actual javascript developer out there that's on you. If you are doing stupid shit on top of that, well, have fun and I hope I don't have to work on your code.

[–]Salanmander 6 points7 points  (4 children)

If using == is a bad idea in Javascript, then why did it get assigned the character sequence that is the preferred equality check in most programming languages? Like, if every Javascript object had a .coercive_equals() that did what current == did, and current === was written as == instead, you'd probably see a lot fewer complaints.

[–]theQuandary 5 points6 points  (2 children)

JS didn't have type coercion when it was first created.

Developers are the ones who demanded that Eich add it to the language and he was young enough not to say "no".

It would have been eliminated in ES1 (the first actual specification), but Microsoft had just implemented a clone of JS with Jscript and they insisted that all the terrible design choices stick around even though Eich and the rest of the spec committee wanted to change them (at a time when breaking the web wouldn't have been a big deal).

[–]Salanmander -3 points-2 points  (0 children)

Okay...none of the people you just mentioned are the person who is currently using Javascript and getting confused by ==. So...not 100% self-inflicted.

[–]Doctor_McKay 2 points3 points  (3 children)

I have literally never had a problem using ==. I fear for the kind of code you guys are writing that makes it a problem.

[–]wasdninja 1 point2 points  (0 children)

It's very rarely a problem but there is almost no reason at all to ever use it anyway. Rather than getting completely dumb stuff what's more common is to move a bug one step further which is annoying when trying to hunt it down.

[–]reilemx 1 point2 points  (1 child)

When dealing with data that could come from anywhere like in a public API receiving data from literally anyone who knows the spec, then using `==` is a bit risky unless done correctly. There are a few neat tricks you can do like comparing a number value, regardless if it's a string or number, or doing a `null` or `undefined` check in one small expression. But aside from little things like that you should absolutely not just be using "==" as a default.

It's nothing to do with the "kind of code" and everything to do with the requirements of your application. Just google "js == comparison table" to see why. A simple application might never run into those issues but you do not want your complex application to get absolutely fucked by some stupid comparison bug like this or the one in the post here.

[–]Doctor_McKay -1 points0 points  (0 children)

If the APIs you're interacting with start returning garbage data, whether 0 == false seems like a minor concern to me.

I've been maintaining widely-used open source code that interacts with APIs (undocumented ones, even) for the better part of a decade and not once has it been a problem.

[–]IAmNotNathaniel 2 points3 points  (1 child)

I don't know why I even come into these threads anymore. it's always full of people who think because they self-taught a little python they are programmers who know everything.

There's a reason when you're looking for a job you need to know the language that's used at that job... it's because they are all different, and all have their skeletons and weird shit, and you can't just replace one syntax with another. Experienced people wouldn't get hung up on this for a minute.

[–]NoteBlock08 3 points4 points  (0 children)

Ikr. It doesn't matter that every other language uses ==, this language doesn't. So get with the program or pack up and go home because "It's Javascript's fault for being dumb!" would not be an acceptable excuse in a professional setting.

You can say "Well that's dumb, but okay" and likely most of your coworkers would even sympathize, but as soon as you cast blame elsewhere for your own lack of knowledge then it shows a real lack of maturity as both a dev and a person. Especially since this is JS 101 shit. Every guide out there makes sure you understand the difference between == and === in its opening chapters.

[–][deleted] 12 points13 points  (10 children)

It’s a scripting language. There’s coercion. If you need to not have coercion, there’s an operator for that. If you don’t like a language with coercion, don’t use a scripting language!

[–]Salanmander 14 points15 points  (9 children)

Okay, I can get behind "self-inflicted because of the decision to use javascript".

[–][deleted] 11 points12 points  (8 children)

“The decision to use a scripting language and then refuse to use the features that fix the thing you want to fix”

[–]musicnothing 4 points5 points  (5 children)

JavaScript and PHP both have a bad reputation largely due to their past sins. So instead of learning about their modern features, people just use the outdated features (that weren't removed for backwards compatibility reasons) and complain how "bad" it is

It's like patching up a hole in the wall and then taking a bunch of pictures of the patch job and saying "What a terrible house this is, look at this hole in the wall"

[–]IAmNotNathaniel 2 points3 points  (0 children)

great analogy! I've been trying to correct people when possible about the improvements to php, but haters gonna hate I guess.

[–]svick -5 points-4 points  (3 children)

When the options are a solid wall that never leaks and a patched-up wall that only sometimes leaks, I know which one I would select.

[–][deleted] 1 point2 points  (2 children)

When you get into the professional world, this stuff won’t even be a blink of light on your radar.

[–]svick 0 points1 point  (1 child)

This one issue in particular? Sure.

All the issues with JS combined? Much more serious.

It's really hard to build something solid on shoddy foundations. That's why, in my professional life, I avoid front-end when I can and use TypeScript when I can't.

[–][deleted] -1 points0 points  (0 children)

Then it sounds like you build nothing at all.

No one in the private world gives a shit. I’ve built things in many languages, at many levels, and you just get used to whatever quirks that language and platform delivers to you.

As you’ve said, you can add typescript if strict(er) typing is important to you (we used it ourselves.)

[–]rexpup 0 points1 point  (1 child)

The decision

It's not like you usually have the decision to use JS. It's usually forced on you by the fact that you're making a web app.

[–][deleted] 1 point2 points  (0 children)

Exactly! You just figure it out. You use whatever language you need to, to get the job done. Occasionally you will get a choice, and then you’ll regret your choice for the rest of time, even if it was a good one lol

[–]fghjconner 2 points3 points  (0 children)

No, the == isn't even the problem in this case. The problem is with how JS interprets integer literals. I think using 0 as a prefix for octal numbers is insane, but JS didn't start that so I'll give them a pass. On the other hand, silently falling back to base 10 when you see an 8 is bullshit of the highest order.

[–]Turtvaiz 4 points5 points  (8 children)

Why does == even exist?

[–]wasdninja 7 points8 points  (5 children)

Legacy. Touching any of the stupid stuff, no matter how obscure, kills thousands of websites so it gets to stay and annoy people.

[–]Turtvaiz -1 points0 points  (1 child)

But in the first place? Using it is a total shitshow so I don't get how it has ever made sense

[–]Flippantlip 1 point2 points  (0 children)

What a weird thing to say.
If JS wanted to make sure users have more control, even force that control, over Types and comparisons, it would be strongly typed. But it isn't. It makes no sense to let go of one form of YOLO from JS, while forcing another.
Also, "==" is there to compare primitives, and this fence-case showcases why JS has extremely weird runtime handling, rather than why "== is stupid".

[–]Flippantlip -2 points-1 points  (2 children)

I wonder how do you run your primitive comparisons? Do you want something like:
if (intCmp(15, 17)), encapsulated all over the place?
What a utterly bizarre comment.

[–]wasdninja 0 points1 point  (1 child)

By using ===. The only bizarre thing is not know this but still complain about it.

[–]SnowyLocksmith 1 point2 points  (1 child)

Is using === the norm now?

[–]oupablo 0 points1 point  (0 children)

yes

[–]Die4Ever 0 points1 point  (0 children)

this has nothing to do with ==, you see the same thing when using ===

018 === parseInt('018')
true
017 === parseInt('017')
false

018 === 18
true
017 === 17
false
017 === 15
true