Naming variables
Each variableA memory location within a computer program where values are stored. is named so it is clear which variable is being used at any time. It is important to use meaningful names for variables, for example pocketMoney = 20 means that the variable ‘pocketMoney’ is being used to store how much pocket money you have. Right now you have £20. The name given to each variable is up to the programmer, but ideally a variable name should have meaning, eg, it should reflect the value that it is holding.
Variable naming rules
There are some rules about variable names:
- Consistency: ‘name’ is not the same as ‘Name’ or ‘NAME’.
- Spacing: variable names should not have a space in them. Use underscores or camelCaseA capitalisation convention where there are no spaces, and the first letter of the first word is in lower case with the first letters of subsequent words are in upper case. All the ‘humps’ are in the middle. instead, eg total_money; totalMoney).
- Digits: variable names should not start with a digit.
- Variable names should start with a lowercase letter.
Consider these example variable names, all of which could be variable names to store the length of a side of a square:
| Variable name | Comment |
| l | A poor choice – it has no meaning |
| length | Okay but a bit vague |
| side_length | Good |
| sideLength | Good |
| side length | Wrong – don’t use spaces |
| Variable name | l |
|---|---|
| Comment | A poor choice – it has no meaning |
| Variable name | length |
|---|---|
| Comment | Okay but a bit vague |
| Variable name | side_length |
|---|---|
| Comment | Good |
| Variable name | sideLength |
|---|---|
| Comment | Good |
| Variable name | side length |
|---|---|
| Comment | Wrong – don’t use spaces |
Example
This PythonA high-level programming language. (3.x) program uses two meaningful names when calculating the perimeter of a square:
>>> side_length = 5
>>> perimeter = side_length * 4
>>> print(perimeter)
20Because meaningful names have been used in this code, it is easy to know what each variable is used for.
Data types
Variables come in all shapes and sizes. Some are used to store numbers, some are used to store text and some are used for much more complicated types of dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions..
The data typeThe format in which a variable or constant holds data, such as ‘integer’ or ‘string’. to know are:
- String (or str or text). Used for a combination of any characters that appear on a keyboard, such as letters, numbers and symbols.
- Character (or char). Used for single letters.
- Integer (or int). Used for whole numbers.
- Float (or Real). Used for numbers that contain decimal points, or for fractions.
- Boolean (or bool). Used where data is restricted to True/False or yes/no options.
In many programming languageA language used by a programmer to write a piece of software. variables must be declareCreate an empty variable with a specific data type, ready for use. before they can be used, for example:
- Visual Basic -
dim score as int - Java –
int score;
In some languages, such as Python, you can simply start using the variable without declaring it.