Variables (naming, camelCase, UPPER CASE, variables, constants)

Musical Intro

Why variables?

Ever wonder how your favorite video games come to life?

Picture invisible shelves filled with jars, each holding pieces of the game world.

One jar might decide the name of your character, , while another controls the speed of your racing car.

JavaScript
const charachterName = "Bartek";
let maxSpeedOfCar =  200;

In the example above we set the characterName to be “Bartek” and the maxSpeedOfCar to 200.

These jars or variables are at work behind the scenes, making digital experiences thrilling and interactive. They’re the unseen magic that breathes life into pixels and bytes, transforming them into adventures and stories that enthrall us.

Naming variables

The name of a variable has 2 important parts:

  1. Computer friendly name: The computer needs to be able to read the name.
  2. Human friendly name: We coders need to know, what the variable contains

Computer friendly name

For the computer to read it, it needs to be a single text without spaces:

ExampleTypeExplanation
maxSpeedOfCarcamelCaseWe remove all the spaces and each new word starts with a CAPITAL letter.

There is also PascalCase, snake_case, UPPER_SNAKE_CASE, kebab-case, etc. but we will cover them, when the need arise.

Human friendly name

It is also a good idea to give it a name, that we humans can understand, because it will be difficult to understand what this code do:

fn = t + n1 + n2

While this is much easier to read:

fullName = title + firstName + lastName

because we can read, that we take the title putting it together with the first name and the last name and store it as the full name.

Questions about naming

Which variables are ok, and which are not?

JavaScript
const playerName = "Bartek"
const player name = "Bartek"
const player-name = "Bartek"
const pn = "Bartek"
const PlayerName = "Bartek"
Answer

playerName is ok!

player name is not ok! Because the computer don’t like the space.

player-name is not ok! Because the computer don’t like the dash.

pn is not ok! Because we can’t guess what the pn stands for.

PlayerName is not ok! Because variables names must start with a non-capital letter.

Assigning a variable

We assign variables in different ways in different languages.
Just select the language, that you would like to learn. No need to learn all of them.
We use constants, when we don’t want the value to change in the future.
We use variable, when we want the value to change in the future.

Python

Assign a variable: just assign it a value. Use camelCase.
Reassign a variable: Just give a variable a new value.
Assign a constant: just assign it a value. Python don’t support constant, but we can use CAPITAL letters, so we know it is a constant later in the code.
Reassign a constant: Avoid this. If a variable is written in CAPITAL letters, then it is a constant, and should not be changed.

Python
GREETING = "hello" 
name = "Bartek"
name = "Patrick"
GREETING = "hi"

JavaScript

Assign a variable: Use let to assign a variable.
Reassign a variable: Just give it a new value (without let)
Assign a constant: Use const to assign a constant. Use also CAPITAL letters, so we know it is a constant later in the code.
Reassign a constant: This will give an error.
Old school variable: There is also an old version called var. Don’t use it.

JavaScript
const GREETING = "hello";
let name = "Bartek";

name = "Patrick";
GREETING = "hi"; // this will throw an error

Groovy

Assign a variable: use def to assign it a value.
Reassign a variable: Just give it a new value (without def).
Assign a constant: just assign it a value. Groovy don’t support constant, but we can use CAPITAL letters, so we know it is a constant later in the code.
Reassign a constant: Avoid this. If a variable is written in CAPITAL letters, then it is a constant, and should not be changed.

Groovy
def GREETING = "hello" 
def name = "Bartek"
name = "Patrick"
GREETING = "hi"

C#

To assign a constant in C#, we need write const to begin with. Then we need to specify, what type of variable we want. To write text, we need to define it as string and to write a number we write int (more on string and int in later lessons).
We again use CAPITAL letters for constants, to know later in the code, that they are constants.
To assign a variable, we just use string or int.
To reassign a variable, we just give it a new value.
Trying to reassign a constant will give an error.

Assign a variable: use string for text and int for numbers to assign it a value
(more on string and int in later lessons).
Reassign a variable: Just give it a new value, but you can’t change text to number or back.
Assign a constant: use const in front of string or int. Use CAPITAL letters, so we know it is a constant later in the code.
Reassign a constant: This will give an error.

C#
const string GREETING = "hello";
int name = 2;

name = 3;
GREETING = "hi" // this will throw an error
name = "Bartek" // will also throw an error

Challenges introduction

You can use free ChatGPT 3.5 to translate it from Python to other languages (read how)

And use the Web editors: 💻 Python / 💻 JavaScript / 💻 Groovy / 💻 C#

We have this working code example for inspiration, to solve the challenges:

Python
# Given we have 2 numbers: 1 and 2
number1 = 1
number2 = 2

# When we add them together
result = number1 + number2

# Then the result will be printed and asserted
print(result)
assert result == 3

Challenge 1

Can you add the required details, to make the code work?

Python
# Given we have 3 numbers: 1, 2, 3
number1 = 1
number2 = 2
# <add something here>

# When we add them together
result = number1 + number2

# Then the result will be printed and asserted
print(result)
assert result == 6

Challenge 2

Can you add the required details, to make the code work?

Python
# Given we have 4 numbers: 1, 2, 3, 5

# When we add them together

# Then the result will be printed and asserted
print(result)
assert result == 11

Challenge 3

Can you add the required details, to make the code work?

Python
# Given we have 5 numbers: 1, 2, 3, 5, 7

# When we add them together

# Then the result will be printed and asserted

Conclusion

In the journey through the realms of coding, we’ve seen how variables act as the fundamental building blocks, allowing us to store, manipulate, and retrieve data with ease.

Just as jars can hold anything from spices to cookies in the kitchen, variables in programming hold the pieces that bring digital experiences to life, from the names of characters in a video game to the speeds of racing cars.

We learned to name variables, so we know what they actually mean, because anyone can understand ourAge = yourAge + myAge, while nobody can guess what oa = ya + ma means.

We learned to use camelCase for variables, and CAPITAL for constants. And remember not to use spaces, because computers don’t get understand it.

You have also solved 3 challenges – congratulations!

Remember, the power of coding is at your fingertips, and variables are one of the first keys to unlocking that potential. Happy coding!

And you can always create your own challenges, with multiple variables, different values or even using minus, multiplication, or division, instead of plus.

Congratulations – Lesson complete!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.