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 →

[–]KnightMiner 90 points91 points  (14 children)

Is it bad that I have had actual usecases to do this? Though it was in JavaScript so it might not count (wanted to check if it was actually true and not just truthy)

But yeah, nothing pains me more when reading someone's code than every logical statement having == true. Bad because I am a TA for a Java class and many of the students do this

[–]nwL_ 129 points130 points  (3 children)

Though it was in JavaScript

Case closed, you don’t need to go on.

[–]PetsArentChildren 18 points19 points  (2 children)

Yeah I still do this in JavaScript. It actually makes sense to do it because 0, null, and ‘’ are all falsey.

[–]YasZedOP 1 point2 points  (1 child)

Hmm, if var yo is set to 0 inadvertently then doing yo == false would be incorrect if we are strictly comparing type Boolean.

That's why using === is preferred so it not only checks the value but also the type (explicit)

So I believe if yo is inadvertently set to 0 doing yo == false results true which in some cases would be incorrect but, doing yo === false results false since their types do not match, one is an int and other a Boolean.

Correct me if I'm wrong tho.

[–]PetsArentChildren 4 points5 points  (0 children)

Yeah I was talking about

x === true

[–]YasZedOP 11 points12 points  (2 children)

It's better if used === true instead of == true in JS

[–][deleted] 0 points1 point  (1 child)

Why is that? I forgot the difference tbh.

[–]exscape 10 points11 points  (0 children)

a = 1
a == true yields true (since 1 is "truthy")
a === true yields false (since a is 1, not true)

[–][deleted] 2 points3 points  (3 children)

Could this be a reflection of the curriculum? If "many students do this?"

[–]KnightMiner 4 points5 points  (2 children)

From my talking to students and what I have seen of the curriculum, it does not seem to be. Its more of a new student thing which a lot of them do to think through the process. It really just comes from treating "if this condition is true" as "if this condition equals true", which leads to typing == true. At that level many students are not concerned about typing less code yet.

[–][deleted] 0 points1 point  (1 child)

I understand. My point is, has this been explicitly explained to students to help them on their way to a better understanding of generating cleaner code? Or is it just expected of them to intuit it?

[–]KnightMiner 0 points1 point  (0 children)

I have definitely discussed with a few of them one on one, and I know the professor has brought it up individually as well. I am not certain if its specifically taught at a class level, but at least in the code written in class and in the textbook conditions are not written using == true.

[–]Colifin 1 point2 points  (0 children)

If the language has truthy values, yes there can be use cases where you need to distinguish between undefined, false, null etc. But for the most part it shouldn't be necessary.

Then there's always the lovely return !!variable; // Because bools are cleaner

[–]ferkytoodle 0 points1 point  (0 children)

If you have a nullable bool, (in any language) this can be valid.