From owner-freebsd-hackers@FreeBSD.ORG Fri May 1 05:54:21 2009 Return-Path: Delivered-To: freebsd-hackers@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id DB7CA106566B for ; Fri, 1 May 2009 05:54:21 +0000 (UTC) (envelope-from christoph.mallon@gmx.de) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by mx1.freebsd.org (Postfix) with SMTP id 3C7DB8FC08 for ; Fri, 1 May 2009 05:54:20 +0000 (UTC) (envelope-from christoph.mallon@gmx.de) Received: (qmail invoked by alias); 01 May 2009 05:54:19 -0000 Received: from p54A3F073.dip.t-dialin.net (EHLO tron.homeunix.org) [84.163.240.115] by mail.gmx.net (mp008) with SMTP; 01 May 2009 07:54:19 +0200 X-Authenticated: #1673122 X-Provags-ID: V01U2FsdGVkX1+4W/0ZJgS4D8paoqvaloiuCITtly6QI9ugP1ciAm cKwrHQdcNHgRS7 Message-ID: <49FA8E88.1040905@gmx.de> Date: Fri, 01 May 2009 07:54:16 +0200 From: Christoph Mallon User-Agent: Thunderbird 2.0.0.21 (X11/20090412) MIME-Version: 1.0 To: "M. Warner Losh" References: <20090428114754.GB89235@server.vk2pj.dyndns.org> <20090430.090226.1569754707.imp@bsdimp.com> <20090430233648.GA95360@keira.kiwi-computer.com> <20090430.183727.803597558.imp@bsdimp.com> In-Reply-To: <20090430.183727.803597558.imp@bsdimp.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-FuHaFi: 0.5600000000000001 Cc: rick-freebsd2008@kiwi-computer.com, freebsd-hackers@FreeBSD.org Subject: Re: C99: Suggestions for style(9) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 May 2009 05:54:22 -0000 M. Warner Losh schrieb: > In message: <20090430233648.GA95360@keira.kiwi-computer.com> > "Rick C. Petty" writes: > : On Thu, Apr 30, 2009 at 09:02:26AM -0600, M. Warner Losh wrote: > : > > : > This is the biggest one, and I think it may be too soon. Also, we > : > need to be careful on the initialization side of things because we > : > currently have a lot of code that looks like: > : > > : > > : > struct foo *fp; > : > struct bar *bp; > : > > : > fp = get_foo(); > : > if (!fp) return; > : > bp = fp->bp; > : > > : > this can't easily be translated to the more natural: > : > > : > struct foo *fp = get_foo(); > : > struct bar *bp = fp->bp; > : > > : > since really you'd want to write: > : > > : > struct foo *fp = get_foo(); > : > if (!fp) return; > : > struct bar *bp = fp->bp; > : > > : > which isn't legal in 'C'. > : > : I thought we were talking about C99, in which case this is perfectly legal. > : I certainly use it all the time in my C99 code. > > Hmmm, looks like that was added. That's ugly as C++... I do not think, this is ugly. On the contrary, it aids maintainability, because it reduces the scope of variables. Also quite some variables are only initialised and not changed afterwards, so it's nice to have the declaration and the only assignment in a single place. IMO this is a quite natural style, because you don't have anything in the code, before it is needed: Get the first pointer; if something is wrong, bail out; get the second pointer - the second pointer does not (textually) exist before it is needed. > : And I thought this was the point of this discussion, to be able to declare > : variables when you first use them. > > That's one of the proposed changes, which I think is a mistake and > would cause the most code churn. And it isn't one of the items that's > being discussed: only moving variables into inner scopes is on the > table... No, this is not what I intended. The idea is to limit the scope of local variables as much as is sensible. Maybe I should have been more explicit. On the other hand, I also did not mention that it is just about moving to the start of inner block statements. Christoph