Re: Thoughts on C version supported for PHP-Next
| From: | Andrea Faulds | Date: | Mon, 28 Jul 2014 14:19:25 +0000 |
| Subject: | Re: Thoughts on C version supported for PHP-Next | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-76234@lists.php.net to get a copy of this message | ||
On 28 Jul 2014, at 15:00, Derick Rethans <derick@php.net> wrote:
>> Well, we don’t need to allow all of C99. We can simply allow using
>> features that are widely supported and actually useful. For example,
>> declarations between statements,
>
> I think that makes code a lot less readable, so I would not be in favour
> allowing this in our coding guidelines.
Really? I’ve found that declaring everything in advance makes for hard-to-read code. To take an
example from code I’ve written (admittedly not C code - GML if anyone’s curious - but it might
as well be), the first line of a function looked like this:
var list, hashList, text, i, pluginname, pluginhash, realhash, url, handle, filesize, progress,
tempfile, tempdir, failed, lastContact, isCached, env;
Even broken onto multiple lines, declaring everything at once isn’t nice. For large functions (and
some functions have to be large necessarily), declaring everything at the start just means a long
list of variables and I would argue impairs code readability.
You should declare variables when and where you need them. If everything is done at the top, you
have to scroll up to check the type of a variable. It also means that now-unused variables are less
obvious as the declarations are far away from the usage, so you probably won’t spot that it’s
useless unless you look at your compiler warnings.
Actually, a similar issue popped up in the code where that example was from. The language it was
written in had some unfortunate properties, one of which was that variables not explicitly declared
would instead be treated as properties of the current object. Because all the variables were
declared at the start of the function, it was easy to miss the fact that some variables being used
had not actually been declared. Of course this specific issue doesn’t exist in C, but it is an
example of why having to declare all your variables at the start of a function isn’t really a good
thing.
If nothing else, I think we should use C99’s ability to declare variables between statements. It
makes it more likely mistakes will be spotted, eases reading code, and as far as I am aware, is
widely supported.
--
Andrea Faulds
http://ajf.me/