Pages: [1]   Go Down
Author Topic: Suggest: Warn on = vs == in "if" statement  (Read 1638 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I bet this is an old topic and there are lots of opinions on the subject.  But I can't tell you how many times I've inadvertently typed "if (x=y)" when I meant "if (x==y)" and then wasted time trying to figure out what was wrong until I spotted this typo.  I can only imagine how many beginners get nailed by this over and over.

Of if we don't like warnings, another suggestion would be a compiler switch that by default doesn't allow a single = in an "if" unless you override the switch.  Only experienced programmers are going to demand assignment within an "if" anyway and this surely seems like something where we're unnecessarily burdening newbies.

Charlie
Logged

France
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3031
There is an Arduino for that
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As far as I know errors and warnings are returned by the compiler which development is not in charge of arduino team.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 553
Posts: 46286
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are times when = is perfectly valid in an if test:
Code:
int cnt, i, j, k;
char buffer[] = "1 2 34";
if((cnt = sscanf(buffer, "%i %i %i", &i, &j &k)) > 2)
{
  // buffer contained 3 integers or more
}
Logged

Wigan, UK
Offline Offline
God Member
*****
Karma: 18
Posts: 814
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

All programming languages are slightly different, I'm not sure it's worth changing everything so it's more familiar with something else.

In pascal, for example, = is the comparison and := sets the variable.

What'd be nice is if the IDE could translate the code from mimicing one language into arduino-suitable code.  So you could set it to c++ syntax, or php syntax and it'd sort it back into arduino syntax.

The lack of $ on variables gets me every time I'd doing any development involving php and the arduino, though that's more missing them off in php and wondering why nothing's working.
Logged

Louisville, CO
Offline Offline
Full Member
***
Karma: 2
Posts: 174
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can swap operands in comparison,  like if( 4 == x ... I know several people writing like that. If you make a mistake and type 4 = x you'll receive an error from the compiler.

Works for constants only, though.
Logged

/felis

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can only use a single = sign if there is an object that returns a Boolean value can't you?  Paraphrasing ftw. You realise if we had a full IDE this would be pointed out to us... Cough cough visual studio...
Logged

Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You can only use a single = sign if there is an object that returns a Boolean value can't you?
No. Boolean means 0 or 1, it actually looks whether the value is 0, if it is, it returns false, otherwise true*. The caveat of this is that it accepts lots of values as true, and only 0 as false. So '0 or 1' is actually incorrect.. from a technical standpoint, logic wise, it holds up ('unset' or 'set').
Also, you can simply use a boolean operator on the returned value, the returned value will be stored and then checked against the boolean operator (make sure to use brackets around the value assignment, or it'll do the boolean operation first.. which is also a perfectly legit use).

If visual studio whines about an assignment in an if, I'll never touch visual studio again. It is perfectly valid coding.. can make reading certain complex control structures readable too.


* when testing this, you can't test it like this:
if(100 == true)
Because, 100 doesn't equate to the 1 that the true constant has been defined as, instead you should test it like this:
if(100)
This one will pass the check, and execute the body of the if, if a value of 0 is used in the if statement, it won't execute the body.
Logged

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ooh, ok.  What about if true were in caps, so it is compiled as the C meaning as opposed to the Arduino interpretation of #define true 1 ?
Logged

Holland
Offline Offline
Sr. Member
****
Karma: 0
Posts: 439
Arduino likes cookies too
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I assume also defined as 1, haven't tested it, but it is expected behaviour.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
so it is compiled as the C meaning

There is no C meaning of TRUE.
TRUE is not a C keyword, and can be defined as you wish.

My favourite is :
Code:
#define FALSE (0)
#define TRUE (!FALSE)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

ottawa, canada
Offline Offline
God Member
*****
Karma: 5
Posts: 989
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
As far as I know errors and warnings are returned by the compiler which development is not in charge of arduino team.

ok so the suggestion is a tool menu item that would scan for common errors.  

Similarly, I wonder if it would be possible to have a tool that would check that the port/board selected was workable.
Logged

Devon, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 234
Arduino rocks my socks, yes the socks are rocking!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
There is no C meaning of TRUE.
TRUE is not a C keyword, and can be defined as you wish.

Well, I'll be, I always thought things like true and false were intrinsic to a language.  Learn something new / correct something old (smiley-razz) every day!
« Last Edit: July 12, 2010, 06:12:33 pm by Gray_Malkin » Logged

Pages: [1]   Go Up
Jump to: