Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
What makes a good variable name?

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

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

An Introduction to Standards in Perl
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [2875] A long day in Melksham ... - (2010-07-17)
  [2688] Security considerations in programming - what do we teach? - (2010-03-22)
  [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 - Applying OO design techniques and best practise
  [4718] Defining an object that is a modified standard type in Python - (2016-11-02)
  [4359] How to avoid too many recalculations within an object - (2014-12-21)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [3887] Inheritance, Composition and Associated objects - when to use which - Python example - (2012-10-10)
  [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
  [4208] Tcl + Tk (Wish) - an introduction and revision example - (2013-11-15)
  [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
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3067] Using C and C++ functions in the same program - how to do it - (2010-11-24)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2646] Compile but do not run PHP - syntax check only - (2010-02-22)
  [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
  [4305] Learning to program in Java - yes, we can help. - (2014-09-26)
  [2653] Java Server Pages - putting it all together - (2010-02-26)
  [2525] Using JSPs, Tag Libraries, Java Beans, Tomcat in one short example - (2009-12-04)

Designing PHP-Based Solutions: Best Practice
  [4691] Real life PHP application using our course training MVC example - (2016-06-05)
  [4641] Using an MVC structure - even without a formal framework - (2016-02-07)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [4118] We not only teach PHP and Python - we teach good PHP and Python Practice! - (2013-06-18)
  [4069] Even early on, separate out your program from your HTML! - (2013-04-25)
  [3926] Filtering PHP form inputs - three ways, but which should you use? - (2012-11-18)
  [3820] PHP sessions - a best practice teaching example - (2012-07-27)
  [3813] Injection Attacks - PHP, SQL, HTML, Javascript - and how to neutralise them - (2012-07-22)
  [3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
  [2679] How to build a test harness into your PHP - (2010-03-16)
  [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.
  [4324] Learning to program - variables and constants - (2014-11-22)
  [4271] Line, block and nested comments - Lua compared to other languages - (2014-05-04)
  [3724] Learning to Program in Lua - public / open training course / class - (2012-05-09)
  [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.

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/solutions/general- ... -name.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard