BACKGROUND
All the programming languages that we teach use variables and although some things differ, they're common in that they provide a way to name a memory location for use later on. And they're common (in the languages that we teach) in that a variable name is almost always ...
a letter
followed by letters, digits and underscores.
In the case of PHP, variable names are preceded by a $; in the case of Perl, they're preceded by a $ a @ a % an & or a *. In the case of Tcl, they're preceded by a $ when being used but not when being set.
In all the languages that we teach, a 52 letter alphabet is used - in other words, variable names are case sensitive.
Why do we care so much about variable names to write a complete article about them? Because a good choice of variable names can turn an unreadable and un-maintainable piece of code into one that's well documented and easy to debug. Commenting your program is very important, and using descriptive variable names in many ways is just as significant.
As you set your own standards (please use my document here as a guideline) think ...
Will my variable names by understood
Will they provide information to the maintenance programmer
Will they be easy to get right as more code is written
Will they get confused with one another
CASE AND UNDERSCORING
Yes, I know you can mix upper and lower case - but do you really want to? It's so easy to use upper case for one variable, lower case for another, then to forget which you used later on.
Suggestion - keep all your "simple" variables to lower case throughout. Depending on the language you're using, you may wish to capitalise object or class names, or package names. And you may wish to write constants all in capitals.
Should you use underscores in variable names? It's up to you - if your variable name is several words joined together, you may wish (consistently) to place an underscore between words and / or to capitalise the first letter of each embedded word - sometimes known as "camel case".
In Python, you're recommended to start a private variable name with a single underscore, and an intensely private name with a double underscore. System names start AND end with double underscore.
LENGTH
It's so easy when you learn to program to use variable names like "i" and "j" but this will make your longer pieces of code almost impossible to maintain later if you continue to do so throughout. I've heard this described as "job protection" coding.
A second convention is to use abbreviations for variables - all programmers are lazy when it comes to typing, so perhaps you would prefer to write (as I once did in an example) $n/f rather that $number_pieces_fruit. But guess which of those is easier to understand later!
I'm going to recommend that you use longer variable names with full words in them rather than shortenings ... typically, I would expect to see variable names of 6 to 16 characters in length.
BUT ... that's a lot of typing!
a) There's nothing wrong with consistently using a limited number of common shortenings - "no" for "number" for example - so $no_pieces_fruit, and $info is acceptable (or perhaps preferable!) in place of $information
b) For tight code - variables used over just a few lines, such as a loop counter, you may go with variables as short as single letters - almost algebraic names such as $i and $j, or $r and $c (for row and column) or $x and $y (the two axes of a graph).
ENGLISH OR AMERICAN
center or centre? color or colour? capitalize or capitalise?
Your choice but - please - set down an organisation standard that says that "we spell the English way" or "we spell the American way".
See also
Perl Standards
An Introduction to Standards in Perl [2375] Designing your data structures for a robust Perl application - (2009-08-25)
[1863] About dieing and exiting in Perl - (2008-11-01)
[1853] Well structured coding in Perl - (2008-10-24)
[1728] A short Perl example - (2008-07-30)
[1555] Advanced Python, Perl, PHP and Tcl training courses / classes - (2008-02-25)
[1395] Dont just convert to Perl - re-engineer! - (2007-10-18)
[1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
[1221] Bathtubs and pecking birds - (2007-06-07)
[1047] Maintainable code - some positive advice - (2007-01-21)
[965] KISS - one action per statement please - Perl - (2006-12-05)
[945] Code quality counts - (2006-11-26)
[743] How to debug a Perl program - (2006-06-04)
[668] Python - block insets help with documentation - (2006-04-04)
[242] Satisfaction of training - (2005-03-11)
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)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[945] Code quality counts - (2006-11-26)
[836] Build on what you already have with OO - (2006-08-17)
[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)
[340] Code and code maintainance efficiency - (2005-06-08)
Tcl/Tk - A Review of Tcl and Tk Basics [1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[1174] Installing Tcl and Expect on Solaris 10 - a checklist - (2007-05-02)
[1092] Tcl training - often for a larger group - (2007-02-24)
C and C based languages - Putting it all together [1181] Good Programming practise - where to initialise variables - (2007-05-09)
[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)
Java - Putting it all together [2525] Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example - (2009-12-04)
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)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[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)
[945] Code quality counts - (2006-11-26)
[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)
Introduction to Lua. [2310] Learning to write high quality code in Lua - (2009-07-30)
[1842] Lua Course, and the Wiltshire Countryside too - (2008-10-16)
[1737] Rules, suggestions, considerations for Lua variable names - (2008-08-05)
[1699] If you are learning Lua, here are some more examples - (2008-07-06)
[1695] Lua Course - here is the index - (2008-07-03)
[1111] Training in Lua - (2007-03-16)
resource index - Deployment
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.