Go Down

Topic: Suggest: Warn on = vs == in "if" statement (Read 2 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

Go Up