Suggest: Warn on = vs == in "if" statement

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

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

There are times when = is perfectly valid in an if test:

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
}

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.

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.

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

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.

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 ?

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

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 :

#define FALSE (0)
#define TRUE (!FALSE)

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.

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!