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/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to