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.
(written 2007-05-09 07:19:33)
Associated topics are indexed under
C239 - C and C based languages - Putting it all together [945] Code quality counts - (2006-11-26)
[925] C++ - just beyond the basics. More you can do - (2006-11-14)
[836] Build on what you already have with OO - (2006-08-17)
H115 - Designing PHP-Based Solutions: Best Practice [2430] Not just a PHP program - a good web application - (2009-09-29)
[2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
[1694] Defensive coding techniques in PHP? - (2008-07-02)
[1623] PHP Techniques - a workshop - (2008-04-26)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1490] Software to record day to day events and keep an action list - (2007-12-31)
[1487] Efficient PHP applications - framework and example - (2007-12-28)
[1482] A story about benchmarking PHP - (2007-12-23)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1390] Converting from postal address to latitude / longitude - (2007-10-13)
[1389] Controlling and labelling Google maps via PHP - (2007-10-13)
[1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
[1323] Easy handling of errors in PHP - (2007-08-27)
[1321] Resetting session based tests in PHP - (2007-08-26)
[1194] Drawing hands on a clock face - PHP - (2007-05-19)
[1182] Painting a masterpiece in PHP - (2007-05-10)
[1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
[1052] Learning to write secure, maintainable PHP - (2007-01-25)
[1047] Maintainable code - some positive advice - (2007-01-21)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
[839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
[572] Giving the researcher power over database analysis - (2006-01-22)
[563] Merging pictures using PHP and GD - (2006-01-13)
[426] Robust checking of data entered by users - (2005-08-27)
[394] A year on - should we offer certified PHP courses - (2005-07-28)
[340] Code and code maintainance efficiency - (2005-06-08)
[261] Putting a form online - (2005-03-29)
[237] Crossfertilisation, PHP to Python - (2005-03-06)
[123] Short underground journeys and a PHP book - (2004-11-19)
J715 - Putting the Java Language TogetherP222 - Perl - Programming Efficiency and Style [2399] Firefighting with Perl - (2009-09-07)
[743] How to debug a Perl program - (2006-06-04)
R119 - Ruby Miscellany [1890] MySQL database from Ruby - an example - (2008-11-16)
[1889] Ruby mixins, modules, require and include - (2008-11-16)
[1720] Some Ruby lesser used functions - (2008-07-26)
[1586] Variable types in Ruby - (2008-03-21)
T248 - Tcl/Tk - A Review of Tcl and Tk Basics [1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
[1092] Tcl training - often for a larger group - (2007-02-24)
Y116 - Python - Best Programming Practice [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[2523] Plan your application before you start - (2009-12-02)
[2485] How do I set up a constant in Python? - (2009-10-31)
[2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
[2363] Alpaca Case or Camel Case - (2009-08-16)
[668] Python - block insets help with documentation - (2006-04-04)
[656] Think about your design even if you don't use full UML - (2006-03-24)
Some other Articles
Themes for the web siteFinding resources - some pointersImproving searches - from OR to AND?Good Programming practise - where to initialise variablesConjugationSizing sheets and other domestic issuesDucking stool for Melksham?Sorting out for a site mapA pu that got me into trouble