View previous topic :: View next topic |
Author |
Message |
Ranfo
Joined: 08 Sep 2008 Posts: 38
|
Warning 201 : Assignment inside relational expression |
Posted: Tue Sep 12, 2017 9:02 am |
|
|
When I do this...
Code: |
if ((mnum = com_delay(duration)) > 0) {
// Do stuff with mnum
} |
I get "Warning 201 : Assignment inside relational expression",
but it appears to work anyway. Should I be concerned?
Is it safe to ignore this warning with CCS? |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Sep 12, 2017 12:09 pm |
|
|
'Tis sloppy, and therefore, 'tis bad.
Code: | mnum = com_delay(duration);
if (mnum > 0) {
// Do stuff with mnum
} |
...'tis better. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Tue Sep 12, 2017 1:16 pm |
|
|
It's perfectly acceptable C.
It is just a warning. It is designed to remind you that a=b in a test, may not be what you want. Separating the assignment and the test ensures this does not happen, but in some cases results in slower code. YPYMATYC. |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Tue Sep 12, 2017 2:00 pm |
|
|
Until you hit a performance wall (be that execution speed and/or code size) I'm of the belief that readable = preferable. I came to hold that belief after I inherited a project from a person who took perverse pleasure in jamming as many statements into a single line as he possibly could.
If the project will never be passed off to another soul, then by all means incorporate space saving shortcuts - if that's your thing. |
|
|
Ranfo
Joined: 08 Sep 2008 Posts: 38
|
|
Posted: Tue Sep 12, 2017 3:15 pm |
|
|
Ttelmah is correct. Not only acceptable, but common. Not sloppy or bad.
Some compilers will do stupid things when warnings are ignored, so that is why I asked. Also, I hate having compiler warnings.
Is is academic at this point because I discovered that I did not want to change the value mnum if the result is 0, so ended up with this:
Code: | v = com_delay(duration);
if (v > 0) {
mnum = v;
// Do stuff with mnum
} |
|
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Sep 13, 2017 12:58 am |
|
|
The above post actually proves newguy's point, by rewriting to get rid of warning you saw the flaw in your thinking. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 13, 2017 1:08 am |
|
|
Quote: | Not only acceptable, but common. Not sloppy or bad. |
It has to do with ones attitude. I always write code in the most simple
way I can (or I think I do), because I realize it will have to be maintained
later. If you don't care about the company you work for and its future,
then you might write code any old which-way. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Wed Sep 13, 2017 5:05 am |
|
|
re:
Quote: | I came to hold that belief after I inherited a project from a person who took perverse pleasure in jamming as many statements into a single line as he possibly could. | hehehe....
I used to do that 30 years ago, so 'guilty'.
When I did the coding , it looked 'fine' and I understood it. Now when I see it, I scratch my head and say 'what is this, who wrote this and what does it do, arrrrrrrrgh' !
'funny' thing is when I coded in assembler, EVERY line was commented, yet when I used BASIC , I got sloppy. Now I'm in the middle of the road, though the older I get the more I need those comments !
Where single line coding shines is FUSES ! I have a separate 'include' file just for fuses. One fuse on one line. I don't type great,so doing fuses ONCE is a good thing since there's more fuses than PIC instructions these dayze !!
Jay |
|
|
|