Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:96852 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 98203 invoked from network); 12 Nov 2016 14:12:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Nov 2016 14:12:36 -0000 X-Host-Fingerprint: 90.208.138.117 unknown Received: from [90.208.138.117] ([90.208.138.117:19001] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E4/C4-35596-35327285 for ; Sat, 12 Nov 2016 09:12:35 -0500 Message-ID: To: internals@lists.php.net References: Date: Sat, 12 Nov 2016 14:12:31 +0000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:43.0) Gecko/20100101 Firefox/43.0 SeaMonkey/2.40 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 90.208.138.117 Subject: Re: [PHP-DEV] C89 vs. C99 From: ajf@ajf.me (Andrea Faulds) Hi Derick, Derick Rethans wrote: > I would want to write down in our coding guidelines that we should *NOT* > do declarations after code, as you will no longer have an overview of > all the types in one place anymore. That's your preference. Personally, I would really appreciate the ability to declare variables later in code. The main reason for this is that it allows me to minimise the number of variables left uninitialised. If you have to declare variables at the top of the function, then many of them will be unitialised for some or all of the lifetime of the function, or initialised to dummy values. This can lead to errors with variables being used before they contain a meaningful value (and undefined behaviour), which is the cause of a lot of bugs in C code (and indeed in PHP's). It's also easier to end up with unused variables this way (whose compiler warnings often get buried or ignored), because you would tend to assume that an unitialised variable is going to be used later. There are other benefits, too. For one, it makes the top of a function less cluttered: you can define variables where you use them, rather than having to list all of them at once. This can make code easier to understand, because you only have to worry about variables which are currently in use, rather than being distracted by variables which might only be initialised or used much later in the function. Plus, because this way more variables are initialised at the point of declaration, it can make it clearer what a variable's purpose is, because you get both the type and the initial value on the same line, rather than possibly on different screens. This makes it easier to spot incorrect types and initial values, too, which is important given C's, hmm, rather weak type system. If you need to find the type of a previously-declared variable, vim's ? command is always at your disposal. But with C99's declarations in the function body, now you might actually know what the variable contains when you use that command. Thanks! -- Andrea Faulds https://ajf.me/