Go Down

Topic: if(a=b) is not if(a==b) could IDE check this b4 upload? (Read 6860 times) previous topic - next topic

prawnstar

Hi

just wanna say that i made a maaaassive noob error today and took like 30 mins to find it

i had an =  instead of an == in an if statement .. . . .

I've done this before and can see myself doing it again, could there be a check run when i hit verify/compile ?  that would save quite some trouble!


just a suggestion :]


westfw

The problem is that "if (a=b)" is a perfectly legal statement.  It just that it doesn't do what the programmer expected.
There actually is a compiler switch that will cause this to issue a "warning" statement, but: the warning is likely to appear pretty cryptic to beginning programmers and other warnings that may or may not be relevant are also likely to appear.

kg4wsv

Yes, please make the IDE psychic, so that it can read my mind and my program will do what I want instead of what I say:)

Sorry, this is just one of those things you have to learn about C.  Sounds like you've learned it...

-j

Graynomad

That's the oldest gotcha in the C book. Welcome to the club :)

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

retrolefty

Quote
That's the oldest gotcha in the C book. Welcome to the club


Indeed. In my case it's the gift that keeps on giving.  :)

Bobnova


The problem is that "if (a=b)" is a perfectly legal statement.  It just that it doesn't do what the programmer expected.

What does it do, other then always return as true and run whatever is in the brackets?

I've made this mistake (too many times, heh), and I'm curious what purpose a=b serves and hence, why it needs to be preserved.

Actually it doesn't always return true. Whatever is "assigned" by the = sign is evaluated by the IF. So
if (myVar = false)
… assigns false to myVar and does not trigger the if statement.
There are 3 kinds of people in the world. Those who are good at maths, and those who aren't.

Graynomad

I think I just though of a usefull way to use this

if (myVar = isMotorRunning())

fewer lines than

myVar = isMotorRunning();
if (myVar)

but presumably the same run time code.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Coding Badly

Quote
but presumably the same run time code


Yup.  Any modern compiler is "smart" enough to recognize that the value has been reused.

Bobnova


James C4S

I understand that it is legal C.  And, I understand that you wouldn't do it on purpose.

However, this is one of those gotchas I think deserves a complier "Warning."  Personally, I can't think of a single time I have ever used assignment inside of an if statement.  And if I had, I wouldn't be offended if the complier simply asked "Hey, did you mean to do this?!"

The opposite deserves some attention as well.  Equality outside of if (or other conditional) statements should throw a Warning as well.

At the *very* least, when a sketch doesn't work if a couple of warnings like these appeared in the IDE it would beg the "uh huh?" question.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Graynomad

I think I should add that although my above example has fewer lines it's actually less clear to a reader, and therefore I think it's fair to say that it's not good coding practice.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

James C4S

Richard, I completely agree with you on #1, but more importantly on #2.

The "value-add" of Arduino is making things easier for "everyone-else."  As you eloquently state, this would be a user-friendly suggestion.  That's all it needs to be.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

James C4S

Quote
therefore I think it's fair to say that it's not good coding practice


Which is why I think it is fair to throw a warning. 
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

prawnstar

well I'm glad the general feeling is that an = in an if should be brought to attention, especially since arduino is meant for n00bs like me :]

Go Up