public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* -Wparentheses lumps too much together
@ 2007-12-19 20:07 jklowden
  2007-12-19 20:14 ` Doug Gregor
                   ` (4 more replies)
  0 siblings, 5 replies; 23+ messages in thread
From: jklowden @ 2007-12-19 20:07 UTC (permalink / raw)
  To: gcc

Hello Gentlemen, 

Much as I'm a fan of the GCC and rely on -Wall, I would like to suggest 
to you that -Wparentheses should be split up, and things it checks/suggests
be moved out of -Wall.  If this is not the right forum or if you'd rather
see this as a bug report, I'm happy to go where I'm pointed.  

The basic problem with this warning is that it includes some helpful advice 
about some subtle bugs with some "dear beginner" advice about "operators ...
whose precedence people often get confused about."  

My assertion is that operator precedence is fundamental to C.  No one can 
read C without knowing.  If you're unsure about precedence, you're only 
guessing.  Moreover, to interpret operator precedence, it's not necessary
to, say, rememember the variables' declarations 100 lines earlier and 
it's impossible to be misled by non-syntactic indentation.  Everything you
need to know to understand the statement is contained in the statement 
itself.  

My specific candidate for exclusion from -Wall is this one:

	if (a && b || c && d)

which yields (as you know) advice to parenthesize the two && pairs.  

I very much think this is unhelpful, counterproductive advice.
Yes, I know beginners get confused by and/or precedence.  But
*every* language that I know of that has operator precedence places
'and' before 'or'.  More important, a C programmer will encounter
many thousands such expressions in his dealings with the language.
To "help" him is merely to retard his education.

But -Wall isn't really concerned with helping beginners, right?  It's really 
about avoiding errors and pitfalls.  That's why I suggest this and other
precedence warnings be moved to -Woperator (notional).  If you need help 
learning C, GCC will help you.  But if you're maintaining 100,000 lines of 
portable, standards-compliant C (as I do), you don't want warnings to 
parenthesize things that don't need it.  

At the risk of overstaying my welcome, let me answer the commonest reply, 
"what's a few parentheses among friends?"  The problem is that, for the 
experienced programmer, "extra" parenthesis are a signal: something 
unusual is going on here.  By forcing your most careful users --
those who bother with -Wall -- to use parenthesis to enforce what the compiler
would do anyway where the meaning was clear(er) without them, you're 
removing a signalling tool by "insisting" on white noise.  I don't think 
that's a good outcome and I doubt it was the intention, but there it is.  

By the way, I distinguish the 'and/or' advice from the more helpful concern:

	while ((erc=foo()) != 0)

because there the programmer's intent is likely impossible to express 
without parentheses; the idiom requires the override.  

One last point.  In looking for the rationale behind this warning, I searched
for examples of it.  I didn't find any discussion on this list.  What I did
find were many examples of people rototilling perfectly fine code, "improving"
it by adding unneeded parenthesis specifically so that it would compile 
cleanly with -Wall.  I think that's a shame: a waste of effort at best.  

I ask you, please, to consider splitting advice about operator precedence from
advice about mismatched if/else branches, and to exclude advice about 
making sure && is parenthesized ahead of || from -Wall.  -Wall is the 
standard for "good, clean code" in many projects.  This warning doesn't 
belong there.  

Thank you for your time and consideration.  

Regards, 

--jkl



^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: -Wparentheses lumps too much together
@ 2008-03-10 17:25 Derek M Jones
  0 siblings, 0 replies; 23+ messages in thread
From: Derek M Jones @ 2008-03-10 17:25 UTC (permalink / raw)
  To: gcc, gdr, iant, jklowden

All,

Developer knowledge of operator precedence and the issue of what
they intended to write are interesting topics.  Some experimental
work is described in (binary operators only I'm afraid):

www.knosof.co.uk/cbook/accu06a.pdf
www.knosof.co.uk/cbook/accu07a.pdf

The ACCU 2006 experiment provides evidence that developer knowledge
is proportional to the number of occurrences of a construct in
source code, it also shows a stunningly high percentage of incorrect
answers.

The ACCU 2007 experiment provides evidence that the names of the
operands has a significant impact on operator precedence choice.

The data from the ACCU06 experiment might be used to select a cutoff
above (ie, frequency of occurrence or developer performance) which
operator pairs will not be flagged as requiring parenthesis.

If GCC wanted to be even more selective it could look at the operand
names before deciding whether to complain.

ps.
I am always on he look out for opportunities to run experiments
using experienced developers.  Does anybody have any suggestions
for conferences I might approach?

-- 
Derek M. Jones                              tel: +44 (0) 1252 520 667
Knowledge Software Ltd                      mailto:derek@knosof.co.uk
Applications Standards Conformance Testing    http://www.knosof.co.uk

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2008-03-10 17:08 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-12-19 20:07 -Wparentheses lumps too much together jklowden
2007-12-19 20:14 ` Doug Gregor
2007-12-19 20:39   ` Ismail Dönmez
2007-12-19 20:15 ` Daniel Jacobowitz
2007-12-19 23:11 ` Ian Lance Taylor
2007-12-20  6:08   ` James K. Lowden
2007-12-20 16:41     ` Paul Brook
2007-12-21 20:19       ` Ross Smith
2008-01-11  7:34         ` Rehno Lindeque
2008-01-11 17:00           ` Joe Buck
2008-01-11 17:05             ` Robert Dewar
2008-01-11 23:26             ` Ian Lance Taylor
2008-01-12  0:50               ` Joe Buck
2007-12-20 18:01     ` Ian Lance Taylor
2007-12-21  5:28       ` James K. Lowden
2007-12-21  9:27 ` Ralf Wildenhues
2007-12-21 17:16   ` NightStrike
2008-01-11 22:44 ` Doug Gregor
2008-01-12  3:48   ` Ian Lance Taylor
2008-01-12 10:16     ` Andreas Schwab
2008-01-12 11:23       ` Paolo Bonzini
2008-01-13 16:03     ` Gabriel Dos Reis
2008-03-10 17:25 Derek M Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).