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)
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.
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 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.
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)
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.
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]
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.
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:
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?
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
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