Go Down

Topic: if(a=b) is not if(a==b) could IDE check this b4 upload? (Read 6856 times) previous topic - next topic

Bobnova

I think it'd be nice to have a warning (even just something in the text box down below, a "Did you really mean this?" line.
Let it compile anyway, don't pop anything up, that way it can be used either way but those of us who are new to C don't have to spend half an hour (or more  :smiley-red:) trying to figure out what is happening.

retrolefty

Well your wish for new or different compiler options are being voiced to the wrong group here. The compiler used by the Arduino is a open sourced project managed by a totaly different group  http://gcc.gnu.org/

You could always contact them and see what they think of your request.

Lefty

Benji

I don't think they'll change the GCC compiler just for a group of newbs, if this should be done, it should be handled during verification in the IDE.
The compiler isn't just used by Arduino users, it is a widely used tool by millions of programmers across the world. They couldn't care less about people like us probably ;)

stimmer

There already is a compiler option for this in GCC (it's part of -Wparentheses). However the IDE disables all warnings (-w). Even in GCC, -Wparentheses is off by default, probably it generates more unhelpful warnings than helpful ones.

Syntax-checking in the IDE is extremely hard to program. You might think it's easy to write a function that finds an = in an if but parsing a string is actually very hard! For example:

if(a=b){....}  needs a warning, but
if(a<=b){...} has one =, but is correct, because the = is part of <=
if(c == '='){...} is correct because the == is correct and the single = is in quotes

Perhaps it would be a better suggestion to make the warning options (or even better, all compiler options) settable in preferences. For absolute beginners, warnings off is a good idea, as they tend to be more confused by the warnings than helped.

Even so, it will never be possible to check for all simple mistakes, there are too many other ones (logical-bitwise confusion & / &&, forgetting braces after the if when you  need it to control multiple statements, or remembering the braces but accidentally putting a ; before them). You've got to learn to take care over every character when coding in C++.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

westfw

Well, we could start making a list of the warning options (like -Wparenthesis) that it might make sense to turn on for at least the user sketch part of the compile.  Programmer types tend to live in a world of 0s and 1s: -Wall to turn on all warnings (causes lots of output even on correct sketches, some due compiler bugs) or -w (all warnings off.)

Another idea is to add a static analysis step (just to the user sketch.  We wouldn't want the core and library authors to have their creativity stifled by having to make their code "technically perfect." (and there are probably things about microcontrollers that SA tools don't understand very well.))

retrolefty

Well the Arduino IDE software doesn't parse and evaluate your program does it? I thought it mostly just adds other text file stuff, adds function headers if needed, and passes it on to the gcc compiler/linker. Asking the IDE to understand the C language is an awful large request and I'm sure would get long stares from any of the Arduino developer members.

So wish away on new warning messages, but only the compiler itself is aware enough to analyze the code and issue warnings. About all the Arduino IDE could do is to pass on different compiler flags to enable or disable the various error message options already available in the gcc compiler.

Lefty

robtillaart


There is a 'good programmers practice' that states if you do a compare of a const with a variable then do it this way

if ( 54 == x) .... iso   if (x == 54) ...

If you forget one = this first one becomes

if (54 = x) .. which generates an error the other doesn't

my 2 cnts
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Graynomad

Quote
do it this way if ( 54 == x)

Good point, yet for some reason I never seem to do it. I must get back into the habit.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

charlieo

I'd like to add another voice to this being an issue.  I'm not a full-time developer and so when I get the time to work on Arduino, I can be a little rusty.  I have wasted more hours than I care to admit tracking down problems that turned out to be precisely this.  I would call it my #1 "d'oh" bug and I've tried to train myself to look for this particular problem whenever things go awry.  Alas, I forget.

Anyway, if turning on compiler options would be an option, I'd be all for it.

Charlie O.

Nick Gammon

I've suggested a couple of times that you get an option to ramp up warnings. Not much response so far.

Here is a test:

Code: [Select]
int main ()
  {
  int a;
  if (a = 1)
    {};  // do something
  return 0;
  }


By default this compiles without errors or warnings. Note however that 1 is assigned to a, and thus the "do something" is always executed.

Now if you add the "-Wall" (all warnings) compiler flag:

Code: [Select]
test.cpp: In function 'int main()':
test.cpp:4: warning: suggest parentheses around assignment used as truth value


A nice warning that you left out an "=" sign.

The feature is there, all we need is an option to enable it.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

rocketgeek

@GrayNomad: Every formal coding standard for C or C++ I've ever read bans deliberate assignments in conditionals for exactly that reason.

@retrolefty: Don't be cruel to the n00bs. A n00b asking about 'fixing' the issue of assignments inside conditionals on one of the gcc dev lists is likely be be flamed into a cinder.

@robtillaart: Many coding standards require that. It is good practice -- perhaps it should go into the documentation?

Don't take this the wrong way, but a big point about is C is surely that it is for grown-ups (by which I mean serious programmers who are expected to stand on their own two feet and code very methodically and carefully), not novices, and it will allow you to do a whole lot of apparently daft things (that other languages forbid or steer you away from for your own safety) on the grounds that you might actually want or need to do them to achieve your goal. So the extreme flexibility brings its own risks.

I'm a C# hack these days (it puts bread on the table) and that language does a lot more hand-holding, but I myself still get caught out by the =/== trap. I 'tsk tsk' myself, raise my eyes to the sky and make yet another note to be more careful in future. However, I have learned from my mistakes and nowadays it is the first thing I look for in a loop or boolean expression that is not working as I intended, so it's no big deal when it does occur.


DCContrarian


Don't take this the wrong way, but a big point about is C is surely that it is for grown-ups (by which I mean serious programmers who are expected to stand on their own two feet and code very methodically and carefully), not novices, and it will allow you to do a whole lot of apparently daft things (that other languages forbid or steer you away from for your own safety) on the grounds that you might actually want or need to do them to achieve your goal. So the extreme flexibility brings its own risks.



At the same time, features were put into C that seemed like good ideas at the time but have since proven to be problematic. I doubt if the language were being specified today it would allow assignment in a comparison. It's a historical artifact.

westfw

#28
Feb 21, 2011, 04:08 am Last Edit: Feb 21, 2011, 04:20 am by westfw Reason: 1
Quote
a big point about is C is surely that it is for grown-ups

Bah.  The "grown ups" turn on all the warnings, run multiple static analysis tools in addition to the compiler, have access to complex debugging tools, and have other grown-ups review their code...  (and their code is still full of bugs.)

It would certainly be a worthy goal if the Arduino core and core libraries all compiled without messages with -Wall turned on.  I think there are a couple of compiler bugs that prevent this from being 100% possible...

Well, C was written to help write an operating system (UNIX), where you are hitting the hardware and want to be able to do pretty much anything. It could be argued that if you are not writing an operating system you shouldn't be using C. I suspect the choice for Arduino was dictated largely by the need for a free, widely understood language/compiler, not because C is the most appropriate language for the job - teaching novices how to program a controller.

And now I'll duck. :-)



Go Up