Go Down

Topic: Suggest: Warn on = vs == in "if" statement (Read 2141 times) previous topic - next topic

charlieo

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

fdufnews

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

PaulS

There are times when = is perfectly valid in an if test:
Code: [Select]
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
}

CowJam

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.

felis

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.
/felis

graymalkin

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

Imahilus

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.

graymalkin

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 ?

Imahilus

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

AWOL

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: [Select]
#define FALSE (0)
#define TRUE (!FALSE)
"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.

bill2009

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.

graymalkin

#11
Jul 13, 2010, 01:12 am Last Edit: Jul 13, 2010, 01:12 am by Gray_Malkin Reason: 1
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 (:P) every day!

Go Up