Pages: [1] 2 3 4   Go Down
Author Topic: if(a=b) is not if(a==b) could IDE check this b4 upload?  (Read 4630 times)
0 Members and 1 Guest are viewing this topic.
Glasgow, scotland
Offline Offline
Newbie
*
Karma: 4
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6373
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 saysmiley

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

-j
Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8443
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 331
Posts: 16516
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley
Logged

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 221
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Maitland, Australia
Offline Offline
Jr. Member
**
Karma: 1
Posts: 88
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

There are 3 kinds of people in the world. Those who are good at maths, and those who aren't.

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8443
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but presumably the same run time code

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

Humboldt, CA
Offline Offline
Full Member
***
Karma: 2
Posts: 221
Arduino BBB
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting, that makes sense.
Thanks!
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8443
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Glasgow, scotland
Offline Offline
Newbie
*
Karma: 4
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3 4   Go Up
Jump to: