From: Ian Campbell Subject: Re: [PATCH RFC v2 06/13] libxl: permit declaration after statement Date: Tue, 29 Nov 2011 10:45:38 +0000 Message-ID: <1322563538.20646.36.camel@zakaz.uk.xensource.com> References: <1319827031-15395-1-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-2-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-3-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-4-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-5-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-6-git-send-email-ian.jackson@eu.citrix.com> <1319827031-15395-7-git-send-email-ian.jackson@eu.citrix.com> <1320054652.23193.28.camel@zakaz.uk.xensource.com> <20145.29085.278257.629473@mariner.uk.xensource.com> <1320263024.3084.61.camel@cthulhu.hellion.org.uk> <20179.47462.815671.301622@mariner.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <20179.47462.815671.301622@mariner.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: Ian Jackson Cc: "xen-devel@lists.xensource.com" , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org On Mon, 2011-11-28 at 16:40 +0000, Ian Jackson wrote: > Stefano Stabellini writes ("Re: [Xen-devel] [PATCH RFC v2 06/13] libxl: permit declaration after statement"): > > I think it would make sense to add to the CODING_STYLE that variable > > declarations shouldn't be mixed with code, unless part of a macro or an > > alloca-like construct. > > I think this is a silly rule. In my proposed commit message I > advanced three reasons for allowing declaration after statement: > > > > * It allows variables to be more often initialised as they are > > > declared, thus reducing the occurrence of uninitialised variable > > > errors. > > > > > > * Certain alloca-like constructs (arrays allocated at runtime on the > > > stack) can more often be written without a spurious { } block. > > > Such blocks are confusing to read. > > > > > > * It makes it easier to write and use macros which declare and > > > initialise formulaic variables and do other function setup code, > > > because there is no need to worry that such macros might be > > > incompatible with each other or have strict ordering constraints. > > Of these the first two improvements would be banned by your proposed > coding style rule. > > I don't understand what the harm is in allowing declarations, with > initialisation, freely mixed with code. Variable declarations should either be at the top of the function or immediately before / as part of the block which uses them. e.g. this is unhelpful: void a_func(void) { /* a bunch of code; */ int bar; /* a bunch more code not using bar */ bar = get_me_a_bar(); /* use bar lots */ } Ian.