From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22423 invoked by alias); 21 Dec 2007 19:32:44 -0000 Received: (qmail 22411 invoked by uid 22791); 21 Dec 2007 19:32:42 -0000 X-Spam-Check-By: sourceware.org Received: from Unknown (HELO smtp1.ihug.co.nz) (203.109.136.101) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 21 Dec 2007 19:32:16 +0000 Received: from cust.filter2.content.ihug.net.nz (smtp.mailfilter2.ihug.co.nz) [10.80.50.2] by smtp1.ihug.co.nz with esmtp (Exim 4.60 #1 (Debian); Ihug conf #216) id 1J5nbC-0000rX-IS; Sat, 22 Dec 2007 08:31:58 +1300 Ironport-Content-Filter: send-to-smtp Received: from 203-118-185-251.dsl.dyn.ihug.co.nz (HELO [10.1.1.5]) ([203.118.185.251]) by smtp.mailfilter2.ihug.co.nz with ESMTP; 22 Dec 2007 08:31:56 +1300 Message-ID: <476C1461.2050107@ihug.co.nz> Date: Fri, 21 Dec 2007 20:19:00 -0000 From: Ross Smith User-Agent: Thunderbird 2.0.0.9 (Windows/20071031) MIME-Version: 1.0 To: Paul Brook CC: gcc@gcc.gnu.org, "James K. Lowden" , Ian Lance Taylor Subject: Re: -Wparentheses lumps too much together References: <20071219200235.GA21525@oak.schemamania.org> <20071220005030.4971a442.jklowden@freetds.org> <200712201509.20582.paul@codesourcery.com> In-Reply-To: <200712201509.20582.paul@codesourcery.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2007-12/txt/msg00650.txt.bz2 Paul Brook wrote: >James K. Lowden wrote: >> >> 1) most combinations of && and || don't need parentheses because >> >> (a && b) || (c && d) >> >> is by far more common than >> >> a && (b || c) && d >> >> and, moreover, broken code fails at runtime, and > > I dispute these claims. > > The former may be statistically more common, but I'd be surprised if the > difference is that big. I can think of several fairly common situations where > both would be used. > > Any time you've got any sort of nontrivial condition, I always find it better > to include the explicit parentheses. Especially if a, b, c, and d are > relatively complex relational expressions rather than simple variables. I second Paul's points. The precedence of && and || are not widely enough known that warning about it should be off by default (for people sane enough to use -Wall). A couple of data points: First, I've been writing C and C++ for a living for nearly 20 years now, and I didn't know that && had higher precedence than ||. I vaguely recalled that they had different precedence, but I couldn't have told you which came first without looking it up. I'd happily bet that the same is true of the overwhelming majority of developers who aren't compiler hackers. Giving && and || different precedence is one of those things that feels so totally counterintuitive that I have trouble remembering it no matter how many times I look it up. I have a firm coding rule of always parenthesising them when they're used together. (Likewise &, |, and ^, which have similar issues. I can't remember whether -Wparentheses warns about those too.) Second, I just grepped the codebase I'm curently working on (about 60k lines of C++) for anywhere && and || appear on the same line. I got 49 hits, 29 where && was evaluated before ||, 20 the other way around. (All of them, I'm happy to say, properly parenthesised.) So while &&-inside-|| seems to be slightly more common, I'd certainly dispute James's claim that it's "far more common". -- Ross Smith