refuse implicit operator precedence

Hello,

Can i have the arduino compiler shout whenever an expression uses implicit precedence (and request me to put parentheses to force the evaluation order)

e.g. refuse a<<8+b and request (a<<8)+b or a<<(8+b)

Thanks
Edit Aug. 10
The short answer is YES (almost). One can have a compiler warning for dyadic ops with loose parenthesizing on newer Arduino IDE version (tried with 1.6.5)

How would the compiler know what you really meant ?
Why not put the parentheses in anyway ?

Here are a few spare ones that I found laying around (((((((((())))))))))
Please feel free to use them.

Unfortunately I don't think there is a warning level high enough to warn you about implicit precedence. You'll have to learn and remember the odd cases just like the rest of us. :frowning:

You'll have to learn and remember the odd cases just like the rest of us.

Or get in the habit of always using parentheses.

OP: you could try the -prorectMeFromMyOwnCarelessness compiler option. Not all compilers support this option, though.

How sad, The purpose of a compiler is (also) to protect me from my carelessness and the implicit precedence is a source of error that can be avoided simply by ... refusing such expressions :slight_smile: For a compiler, nothing easier to detect and for a human nothing easier to not pay attention to and create the wrong code.

There would be little point in even implementing operator precedence if the compiler insisted you ALWAYS force the evaluation order using parentheses. Proper use is simple: If you can remember the precedence, the you can OMIT the parentheses. If not, then USE parentheses. For the sake of readability, it is best to simply always use parentheses, to make your intent crystal clear.

Regards,
Ray L.

The purpose of a compiler is (also) to protect me from my carelessness

That is not and never can be the compilers job!

the implicit precedence is a source of error that can be avoided simply by ... refusing such expressions

It is not a source of error only humans are - in this case you.

Mark

holmes4:
That is not and never can be the compilers job!
It is not a source of error only humans are - in this case you.

Mark

well dear we are in disagreement

RayLivingston:
If not, then USE parentheses.

I don't know all precedences and so i tend to use parentheses except perhaps when it's natural as in writing a math formula as y=ax+b however, i can FORGET and this is what happened and it resulted in a broken software (surveillance stopped while i was abroad)

guy_c:
I don't know all precedences and so i tend to use parentheses except perhaps when it's natural as in writing a math formula as y=ax+b however, i can FORGET and this is what happened and it resulted in a broken software (surveillance stopped while i was abroad)

Shouldn't that be "y = a * x + b;"? :slight_smile:

RayLivingston:
There would be little point in even implementing operator precedence if the compiler insisted you ALWAYS force the evaluation order using parentheses. Proper use is simple: If you can remember the precedence, the you can OMIT the parentheses. If not, then USE parentheses. For the sake of readability, it is best to simply always use parentheses, to make your intent crystal clear.

Regards,
Ray L.

I was with you until the last sentence. I'm not convinced that a pile of parentheses will always improve readability. Sometimes I use whitespace instead of parentheses, where the precedence is based on operators, but needs to be highlighted for clarification. If you are in doubt about operator precedence, you can always look it up. Then you will probably remember next time. The precedence rules were quite well chosen for readability.

I still don't see how the compiler can be expected to know what you meant to do in your code. Can you provide an example of what you would like it to do ?

i can FORGET and this is what happened and it resulted in a broken software (surveillance stopped while i was abroad)

So, any code which contains a bug is the fault of the compiler!
All these many years I've been learning how to avoid and/or debug my coding mistakes when all I need to do is blame the compiler. This is really liberating!

  • forget that C arrays start at zero? Compiler error.
  • forget to ensure that you don't run off the end of an array? Compiler error.
  • forget to initialize a pointer? Compiler error.
  • forget that division of two integers gives an integer? Compiler error.
  • etc.

Pete

On a more serious note.
@guy_c
Do you have verbose compiler warnings turned on in preferences AND do you always check the warning messages?
If you did, you would see that code like this:

  int a=3,b=6,c;
  
  c = a<<8+b;

produces this warning:
sketch_aug07a.ino:5:11: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]

Pete

What would eventually happen is that when you write something you know is correct, like:

a = b * 2 + c;

Where we all know multiplication is done before addition.

  1. You leave off the parentheses and get the warning you want to have, and get them everywhere, so that you start ignoring them. Once you start ignoring warnings they may as well not exist.

or:

  1. You put parentheses everywhere and make the code unreadable.
a = (b * 2) + c;

OK that wasn't too bad. But in a longer expression? So one day you will write:

a = b * (2 + c);

(By mistake, because you are in the groove of typing brackets all the time). Then you will complain that the compiler didn't warn you that whilst it did what you wrote, you obviously didn't mean it.

el_supremo:
On a more serious note.
@guy_c
Do you have verbose compiler warnings turned on in preferences AND do you always check the warning messages?

No and this is precisely what i was looking for. I tried to check in file > preferences the verbose box but it it only resulted in displaying program chain invocation but not warnings. Can you please help?

I'll try again. Let's say you want to do:

a = b + 1;

Do you want a warning there?

"Of course not!" you say! "That would be ridiculous!"

Well, these all compile:

int main ()
  {
  int * a;
  int * b;
  a = b + 1;    // I wonder what precedence will happen here?
  (a = b) + 1;
  a = (b + 1);
  }

So if you want warnings everywhere you will get them on practically every line of code. And then you will start ignoring them.

You have to remember some stuff. Like "*" means multiply and "/" means divide. Sorry about that chief.

You have to learn stuff, remember stuff. The compiler can't protect you from yourself, completely.

guy_c:
No and this is precisely what i was looking for. I tried to check in file > preferences the verbose box but it it only resulted in displaying program chain invocation but not warnings. Can you please help?

You seem to have set:

Show verbose output during:  [X] compilation  [ ] upload

You want the line below that:

Compiler warnings:  [ All          ]

johnwasser:
You seem to have set:

Show verbose output during:  [X] compilation  [ ] upload

You want the line below that:

Compiler warnings:  [ All          ]

I dont see this line and in preferences.txt ih have compiler.warning_level=default

guy_c:
I dont see this line and in preferences.txt ih have compiler.warning_level=default

If you are going to skip the Preferences dialog and edit the preferences.txt directly you would change that to:

compiler.warning_level=all

this is definitively strange!

I don’t not have the option to change the warning level in the preferences dialog (see screenshot in previous post) nor does changing the text file from “default” to “all” change anything to the compilation messages