From owner-freebsd-arch@FreeBSD.ORG Tue Feb 1 20:43:51 2005 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id AA0BB16A4CE for ; Tue, 1 Feb 2005 20:43:51 +0000 (GMT) Received: from mx1.originative.co.uk (freebsd.gotadsl.co.uk [81.6.249.198]) by mx1.FreeBSD.org (Postfix) with ESMTP id DD91943D41 for ; Tue, 1 Feb 2005 20:43:48 +0000 (GMT) (envelope-from paul@mx1.originative.co.uk) Received: from localhost (unknown [127.0.0.1]) by mx1.originative.co.uk (Postfix) with ESMTP id A10B615575; Tue, 1 Feb 2005 20:43:47 +0000 (GMT) Received: from mx1.originative.co.uk ([127.0.0.1])port 10024) with ESMTP id 98052-04; Tue, 1 Feb 2005 20:43:31 +0000 (GMT) Received: by mx1.originative.co.uk (Postfix, from userid 1000) id 9D60115579; Tue, 1 Feb 2005 20:43:31 +0000 (GMT) Date: Tue, 1 Feb 2005 20:43:31 +0000 From: Paul Richards To: Marcel Moolenaar Message-ID: <20050201204331.GH61409@myrddin.originative.co.uk> References: <20050128173327.GI61409@myrddin.originative.co.uk> <20050131102630.GJ61409@myrddin.originative.co.uk> <20050201180624.GB19624@funkthat.com> <20050201190416.GG61409@myrddin.originative.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i cc: arch@freebsd.org Subject: Re: c99/c++ localised variable definition X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 01 Feb 2005 20:43:51 -0000 On Tue, Feb 01, 2005 at 12:30:37PM -0800, Marcel Moolenaar wrote: > On Feb 1, 2005, at 11:04 AM, Paul Richards wrote: > > >>And wonder why i gets such a strange value... It appears that unless > >>you have WARNS=4 set, warnings about: > >>t.c:10: warning: declaration of 'i' shadows a previous local > >> > >>don't show up. So, I would say we HAVE to get the tree building with > >>WARNS=4 and -Werror before we let this into style(9)... > > > >The issue with shadowing outer scope variables is only an issue if > >you need to access them. If your only using the syntax for loop > >variables to do the looping then there's no issue. > > Never forget that you want to be able to debug you application. > While technically you're right, it's bad practice to do so. I disagree with this point. I think it's more error prone to rely on a side effect of a loop variable to exist outside the scope of the loop. I know that C programmers are used to this, but from the perspective of good programming practice I don't think it's a good one. Of these two situations: Case 1: int i; for (i = 0; i < MAX; i++) if (i == 5) break; printf("Found %dth element\n"); Case 2: int fifth_element = 0; for (int i = 0; i < MAX; i++) if (i == 5) { fifth_element = i; break; } } if (fifth_element) printf("Found %dth element", fifth_elemtn); The latter seems the better written code to me. It's more verbose, but it's clearer what's being done. It seems pointless when you consider it in isolation but since probably more than 99% of for loops only declare outer scope variables in order to terminate the loop the above is the exception and not the rule and for the other 99% of the time using localised looping variables is better since it doesn't pollute the outer scope. -- Paul Richards