if(a=b) is not if(a==b) could IDE check this b4 upload?

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 :]

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.

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. :slight_smile:

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

-j

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


Rob

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. :slight_smile:

westfw:
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.

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

but presumably the same run time code

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

Interesting, that makes sense. Thanks!

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.

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

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.

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.

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 :]

I think it'd be nice to have a warning (even just something in the text box down below, a "Did you really mean this?" line. Let it compile anyway, don't pop anything up, that way it can be used either way but those of us who are new to C don't have to spend half an hour (or more :blush:) trying to figure out what is happening.

Well your wish for new or different compiler options are being voiced to the wrong group here. The compiler used by the Arduino is a open sourced project managed by a totaly different group http://gcc.gnu.org/

You could always contact them and see what they think of your request.

Lefty

I don't think they'll change the GCC compiler just for a group of newbs, if this should be done, it should be handled during verification in the IDE. The compiler isn't just used by Arduino users, it is a widely used tool by millions of programmers across the world. They couldn't care less about people like us probably ;)

There already is a compiler option for this in GCC (it’s part of -Wparentheses). However the IDE disables all warnings (-w). Even in GCC, -Wparentheses is off by default, probably it generates more unhelpful warnings than helpful ones.

Syntax-checking in the IDE is extremely hard to program. You might think it’s easy to write a function that finds an = in an if but parsing a string is actually very hard! For example:

if(a=b){…} needs a warning, but
if(a<=b){…} has one =, but is correct, because the = is part of <=
if(c == ‘=’){…} is correct because the == is correct and the single = is in quotes

Perhaps it would be a better suggestion to make the warning options (or even better, all compiler options) settable in preferences. For absolute beginners, warnings off is a good idea, as they tend to be more confused by the warnings than helped.

Even so, it will never be possible to check for all simple mistakes, there are too many other ones (logical-bitwise confusion & / &&, forgetting braces after the if when you need it to control multiple statements, or remembering the braces but accidentally putting a ; before them). You’ve got to learn to take care over every character when coding in C++.

Well, we could start making a list of the warning options (like -Wparenthesis) that it might make sense to turn on for at least the user sketch part of the compile. Programmer types tend to live in a world of 0s and 1s: -Wall to turn on all warnings (causes lots of output even on correct sketches, some due compiler bugs) or -w (all warnings off.)

Another idea is to add a static analysis step (just to the user sketch. We wouldn't want the core and library authors to have their creativity stifled by having to make their code "technically perfect." (and there are probably things about microcontrollers that SA tools don't understand very well.))