Main Content

Good Programming practise - where to initialise variables

Archive - Originally posted on "The Horse's Mouth" - 2007-05-09 07:19:33 - Graham Ellis

It's a good idea to initialise your variables directly before you use them for the first time if you're going to use them as accumulators.

By accumulator I mean that you're going to write assignments such as:
  $n += 4; # Perl
  incr notepad; # Tcl
  lappend flcodes [lindex $line 0] ; # Another Tcl example
  $html .= $nextline; # PHP
  sofar = sofar + thistime; # Ruby
  zed += ecs; // C++
  ping = ping + "pong"; /* Java */
  build = build + house(); # Python

which take the current value of a variable, modify it based on some other input, and resave it to the same location.

In Perl, fail to initialise a variable and it will be assumed to be empty (normally a good assumption) and the same applies in PHP - except that in older versions of PHP (Prior to 4.1) or if REGISTER GLOBALS is set, it will be filled from any incoming form if it has the same name as a form element. These are usually benign behaviours, so why the advise in this article??

You SHOULD always initialise (initialize) accumulator variables in PHP in case they're used with REGISTER GLOBALS on, as failure to do so renders you open to an injection attack by knowledgabe users. And you'll see a great deal of code that initialises some (or all) variables at the start. I'm going one step further - I'm advocating proximity initialisation

Scenario. You write a piece of code that analyses a directory. Or the contents of a file. Or loops through an array. You test it, and it works well. In fact, it works so well that you put it into a loop to analyse a whole series of directories / files / arrays and it falls over. Why? It could be because an accumulator variable hadn't been reset within the newly added loop.

By adopting proximity initialisation as your coding standard, you can laregly prevent debris rollover from a previous iteration of your newly added loop - although you do need to careful of things like static variables in functions (PHP) too.

Footnote: In Java, Tcl (in most cases, but not with the append and lappend commands, and in Python the language will itself insist on variable initialisation although it won't force you into proximity inialisation. In C and C++, an uninitialised varaible's value often cannot be guaranteed, and many interesting intermittent bugs that take an age to be fixed have been caused by this feature.