What is important that variable names are meaningful?

  • Home
  • News
  • Sport
  • Weather
  • iPlayer
  • Sounds
  • Bitesize
  • CBeebies
  • CBBC
  • Food
  • Home
  • News
  • Sport
  • Reel
  • Worklife
  • Travel
  • Future
  • Culture
  • TV
  • Weather
  • Sounds

  • Home
  • Learn
  • Support
  • Careers
    • My Bitesize

Programming basics

Programming is writing computer code to create a program, in order to solve a problem. To program a computer, you need to know how programs are constructed.

  • Test

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. Page 3of5

Naming variables

Each variable 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, ie 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 camelCase instead, eg total_money; totalMoney).
  • Digits: variable names should not start with a digit

Consider these example variable names, all of which could be variable names to store the length of a side of a square:

Variable nameComment
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

Example

This Python (3.x) program uses two meaningful names when calculating the perimeter of a square:

>>> side_length = 5 >>> perimeter = side_length * 4 >>> print(perimeter) 20

Because 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 data.

The data types 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.
Different data types can occur in a real-life database example. In a film database, string links
to title, float or real links to rating, integer links to times viewed and Boolean links to favourite.

In many programming languages variables must be declared 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.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. Page3of 5

KS3 Subjects

  1. Biology
  2. Chemistry
  3. Citizenship (Wales)
  4. Computer Science
  5. English
  6. French
  7. Geography
  8. German
  9. History
  10. Humanities - Geography (Wales)
  11. Humanities - History (Wales)
  12. ICT
  13. Maths
  14. Modern Foreign Languages
  15. Music
  16. Physics
  17. Religious Studies
  18. Science
  19. Spanish

Why is it important that variable names are meaningful?

Each variable 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.

What is a meaningful variable name?

You can define symbolic variables with meaningful names. Meaningful variable names, like PAY_RAISE, describe the contents of the variable and make CLISTs easy to read and maintain. Note that an ampersand (&) is not part of a variable name; it tells the CLIST to use the value of the variable.

What makes a good variable name?

A good variable name should: Be clear and concise. Be written in English. A general coding practice is to write code with variable names in English, as that is the most likely common language between programmers.

Why is it important to follow a consistent naming convention for variables?

Regardless of how you choose to name your variables, always ensure that your naming conventions are consistent throughout the code. Consistency allows others to more easily understand your code.