Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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, updated 2007-05-11)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y116 - Python - Applying OO design techniques and best practise
  [340] Code and code maintainance efficiency - (2005-06-08)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [668] Python - block insets help with documentation - (2006-04-04)
  [836] Build on what you already have with OO - (2006-08-17)
  [945] Code quality counts - (2006-11-26)
  [2363] Alpaca Case or Camel Case - (2009-08-16)
  [2407] Testing code in Python - doctest, unittest and others - (2009-09-16)
  [2485] How do I set up a constant in Python? - (2009-10-31)
  [2523] Plan your application before you start - (2009-12-02)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [3887] Inheritance, Composition and Associated objects - when to use which - Python example - (2012-10-10)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4359] How to avoid too many recalculations within an object - (2014-12-21)
  [4718] Defining an object that is a modified standard type in Python - (2016-11-02)

T248 - Tcl/Tk - A Review of Tcl and Tk Basics
  [1092] Tcl training - often for a larger group - (2007-02-24)
  [1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
  [1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
  [4208] Tcl + Tk (Wish) - an introduction and revision example - (2013-11-15)

R119 - Ruby Miscellany
  [1586] Variable types in Ruby - (2008-03-21)
  [1720] Some Ruby lesser used functions - (2008-07-26)
  [1889] Ruby mixins, modules, require and include - (2008-11-16)
  [1890] MySQL database from Ruby - an example - (2008-11-16)
  [3155] Rake - a build system using code written in Ruby - (2011-02-03)
  [3428] How many days to Christmas? - (2011-09-09)
  [3622] Loading Ruby classes - where does Ruby look? - (2012-02-24)
  [3783] Load path, load and require in Ruby, and a change from 1.8 to 1.9 - (2012-06-24)
  [3799] Ruby Documentation through rdoc - (2012-07-07)

P222 - Perl - Programming Efficiency and Style
  [743] How to debug a Perl program - (2006-06-04)
  [2399] Firefighting with Perl - (2009-09-07)
  [2657] Want to do a big batch edit? Nothing beats Perl! - (2010-03-01)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)

J715 - Putting the Java Language Together
  [2735] Type checking, Java arrays and collections - (2010-04-23)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [4430] The spirit of Java - delegating to classes - (2015-02-18)

H115 - Designing PHP-Based Solutions: Best Practice
  [123] Short underground journeys and a PHP book - (2004-11-19)
  [237] Crossfertilisation, PHP to Python - (2005-03-06)
  [261] Putting a form online - (2005-03-29)
  [394] A year on - should we offer certified PHP courses - (2005-07-28)
  [426] Robust checking of data entered by users - (2005-08-27)
  [563] Merging pictures using PHP and GD - (2006-01-13)
  [572] Giving the researcher power over database analysis - (2006-01-22)
  [839] Reporting on the 10 largest files or 10 top scores - (2006-08-20)
  [896] PHP - good coding practise and sticky radio buttons - (2006-10-17)
  [936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
  [1047] Maintainable code - some positive advice - (2007-01-21)
  [1052] Learning to write secure, maintainable PHP - (2007-01-25)
  [1166] Back button - ensuring order are not submitted twice (PHP) - (2007-04-28)
  [1182] Painting a masterpiece in PHP - (2007-05-10)
  [1194] Drawing hands on a clock face - PHP - (2007-05-19)
  [1321] Resetting session based tests in PHP - (2007-08-26)
  [1323] Easy handling of errors in PHP - (2007-08-27)
  [1381] Using a MySQL database to control mod_rewrite via PHP - (2007-10-06)
  [1389] Controlling and labelling Google maps via PHP - (2007-10-13)
  [1390] Converting from postal address to latitude / longitude - (2007-10-13)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1482] A story about benchmarking PHP - (2007-12-23)
  [1487] Efficient PHP applications - framework and example - (2007-12-28)
  [1490] Software to record day to day events and keep an action list - (2007-12-31)
  [1533] Short and sweet and sticky - PHP form input - (2008-02-06)
  [1623] PHP Techniques - a workshop - (2008-04-26)
  [1694] Defensive coding techniques in PHP? - (2008-07-02)
  [1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
  [2199] Improving the structure of your early PHP programs - (2009-05-25)
  [2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
  [2430] Not just a PHP program - a good web application - (2009-09-29)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
  [3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
  [3820] PHP sessions - a best practice teaching example - (2012-07-27)
  [3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
  [4069] Even early on, separate out your program from your HTML! - (2013-04-25)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)

C239 - C and C based languages - Putting it all together
  [925] C++ - just beyond the basics. More you can do - (2006-11-14)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4559] When do I use the this keyword in C++? - (2015-10-29)


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?
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
4759 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, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 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).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/1181_Goo ... ables.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb