From owner-freebsd-arch@FreeBSD.ORG Mon Jan 31 12:26:11 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 3CCD716A4CE for ; Mon, 31 Jan 2005 12:26:11 +0000 (GMT) Received: from gizmo03ps.bigpond.com (gizmo03ps.bigpond.com [144.140.71.13]) by mx1.FreeBSD.org (Postfix) with SMTP id B55A443D31 for ; Mon, 31 Jan 2005 12:26:09 +0000 (GMT) (envelope-from andrew@areilly.bpc-users.org) Received: (qmail 4708 invoked from network); 31 Jan 2005 12:26:07 -0000 Received: from unknown (HELO PSMAM03.bigpond.com) (144.135.25.75) by gizmo03ps.bigpond.com with SMTP; 31 Jan 2005 12:26:07 -0000 Received: from cpe-138-130-184-160.nsw.bigpond.net.au ([138.130.184.160]) by PSMAM03.bigpond.com(MAM REL_3_4_2a 89/57423700) with SMTP id 57423700; Mon, 31 Jan 2005 22:26:07 +1000 Received: (qmail 83730 invoked by uid 1000); 31 Jan 2005 12:26:09 -0000 Date: Mon, 31 Jan 2005 23:26:09 +1100 From: Andrew Reilly To: Paul Richards Message-ID: <20050131122609.GA83556@gurney.reilly.home> References: <20050128173327.GI61409@myrddin.originative.co.uk> <20050131102630.GJ61409@myrddin.originative.co.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050131102630.GJ61409@myrddin.originative.co.uk> User-Agent: Mutt/1.4.2.1i 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: Mon, 31 Jan 2005 12:26:11 -0000 On Mon, Jan 31, 2005 at 10:26:30AM +0000, Paul Richards wrote: > 3) Usage in for loops may be more useful than other uses. [snip] > I think the loop usage though is one clear example where it is > clearer. I think there are others as well; where the usage of the > variable is clearly localised it is much easier to see a local > definition than to have to jump back and forth to find out what > variables are. I'd just like to raise a dissenting voice to this particular point. I find the for-loop initialization syntax a pernicious source of errors, mainly because of the non-intuitive scope of the definition. I.e., it looks like it's equivalent to "int i; for (i = 0;;)" but it isn't. If you carelessly c++-ify a loop like: for (int i = 0; i < N; i++) { if (some_condition(i)) break; } do_something_with(i); /* use finishing index */ you can miss the fact that the value of i is used outside of the loop. The newly created scope for "i" shadows the presumably pre-existing definition of i at the top of the function, which is what do_something_with() gets to see. Const friendliness and some minor economy of thought are perhaps valid benefits of this style, but I don't think sufficient to change the style. Cheers, -- Andrew