Mon. Jan 23rd, 2023

Managing Variables

Naming variables

Variables should always be named in accordance with their purpose. For example, a variable that tracks a player’s score in a game should be called ‘score’.

The only exceptions that are generally considered acceptable are when using loops: most programmers will simply use a value such as ‘i’ in a loop. When a variable is only going to be used briefly, this is allowable.

Names of variables should follow these rules:

  • Only start with a letter or an underscore
  • May contain lowercase, uppercase, underscore and digits
  • May not contain spaces
  • Are case sensitive – score is not the same as Score

Additionally, it is common to adopt camel case – this means using lower case, but capitalising the first letter of the second and onward word in a name – examples such as:

  • playerOneScore
  • hasLoggedIn

Scope of variables

Variables fall into one of two groups – they are either local or they are global.

Global Variables

By defining a variable at the top of a script, outside of any functions, it becomes ‘global’ – that is, it is accessible and exists in all of that file. There are many good reasons for using global variables. Some examples are:

  • You want to track a user’s id, and this is used in many functions. A global variable makes sense
  • You are writing a game, and several functions need to be able to access either the score or number of lives a player has

Whatever your use-case, if multiple routines need to access the value, and it should persist throughout the running of the code, you should make it a global variable.

Local Variables

If global variables are so good, why have local ones? Well, any time you only need a variable for a short time, you should use a local variable. For example, if a value is only required while a function is executed, it should be a local variable.

There are many reasons for this approach, but a couple of important ones are:

  • Using local variables means you can re-use variable names elsewhere in your code, without any danger of picking up an erroneous value from use elsewhere (imagine a big piece of software with two hundred FOR loops where each one had to use a different variable name!)
  • It would be very difficult to keep track of every variable name used within a piece of software if all names had to be unique. When you can define variables locally, it makes code far easier to follow.
  • Local variables only consume memory while they are in use. If every variable you ever needed had to reserve and consume memory (a precious resource) for the entire time a program executed, this could cause performance problems

Describing where a variable ‘exists’ is called its scope. In very simple terms, a variable’s scope can be summarised:

  • It only exists within the function it is declared
  • It does not exist in any code that appears to the left of where it is declared (any code that is less indented than the position where it is declared)