Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Good Programming practise - where to initialise variables

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
H115 - Designing PHP-Based Solutions: Best Practice
J715 - Putting the Java Language Together
P222 - Perl - Programming Efficiency and Style
R119 - Ruby Miscellany
T248 - Tcl/Tk - A Review of Tcl and Tk Basics
Y116 - Python - Best Programming Practice

Back to
Conjugation
Previous and next
or
Horse's mouth home
Forward to
Painting a masterpiece in PHP
Some other Articles
Themes for the web site
Finding resources - some pointers
Improving searches - from OR to AND?
Painting a masterpiece in PHP
Good Programming practise - where to initialise variables
Conjugation
Sizing sheets and other domestic issues
Ducking stool for Melksham?
Sorting out for a site map
A pu that got me into trouble
2258 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho