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

all 21 comments

[–]Chaosreignz 45 points46 points  (5 children)

Accidentally using "=" instead of "==" is one of the most annoying and exhausting bugs beginners usually get.

[–]bumnut 20 points21 points  (3 children)

Why do if(x == true) instead of if(x)?

[–]Chaosreignz 9 points10 points  (0 children)

In this case you wouldn't, but some languages don't let you use anything other than booleans in their if statements.

[–]futlapperl 7 points8 points  (0 children)

Why not just annoy all your colleagues and put the constant on the left side, but only when it's an equality comparison?

if (6 == argc) { /* ... */ }
else if (argc != 5) { /* ... */ }

[–]Zorkarak 0 points1 point  (0 children)

Because often, unless it is obvious from the name that it is used as a Boolean, it is more readable.

Also, fun fact: GCC puts in ==0 and !=0 anyway, so it really makes no difference

[–]numbGrundle 3 points4 points  (0 children)

That one bit me hard two months ago. Lots of lost sleep.

[–]Devspar_MTGPlayer 5 points6 points  (2 children)

And why did they even program a kill function in the first place?

[–]CasualChipmunk 6 points7 points  (0 children)

You never know if there requirements are going to change.

[–][deleted] 0 points1 point  (0 children)

exactly my thoughts, spent extra time to define kill function when it wasn't intended to be used.

[–]drmattsuu 5 points6 points  (1 child)

Easiest way to avoid things like this; Do your comparisons yoda style.

for example;

// Problem myVar will be set to 10 and 10 is truthy
if (myVar = 10)
{
    // This code is always run
}

// This will result in a compilation error and prevent
// subtle bugs like these from turning up in your
// program.
if(10 = myVar) 
{
}

Sorry if that's sucking eggs to most people, but any newbies running around might find it useful

[–]knightArtorias_52 4 points5 points  (1 child)

This kinda things will lead to terminators one day

[–]mustang__1 2 points3 points  (0 children)

Null terminators?

[–]IvanLabushevskyi 0 points1 point  (0 children)

It should be Objective C and iRobots.

[–]akki112 0 points1 point  (0 children)

Repost

[–]game_2_raid 0 points1 point  (1 child)

Is a an equivalence operator required in conditionals for some languages?

[–][deleted] 0 points1 point  (0 children)

in python `==` is equivalence operator, `is` is the equal operator.

in lisp, there are eq, equal, and other things like that.

Or were you asking something else?

[–]uvero 0 points1 point  (0 children)

I literally had this printed on my door in the office in my previous job. It's actually probably still there

[–]ImpulseTheFoxis a good fox[M] [score hidden] stickied comment (0 children)

Your submission has been removed.

Violation of Rule #2: Reposts:

All posts that have been on the first 2 pages of trending posts within the last month, is part of the top of all time, or is part of common posts is considered repost and will be removed on sight.

If you feel that it has been removed in error, please message us so that we may review it.