In this tutorial, we will refactor the code we ended with at the end of the tutorial Write a loop in LOGO. “Refactoring is the process of altering an application’s source code without changing its external behaviour. The purpose of refactoring is to improve some of the nonfunctional properties of the code, such as readability, complexity, maintainability and extensibility.” [1]
In this tutorial:
Knowledge required:
1. What is a variable?
In Mathematics, you will have learned to use variables (“placeholders”) in more or less the following manner:
𝑥 + 𝑦 = 5
Solve for 𝑥 if 𝑦 = 2
Then you probably did something like:
𝑥 + 2 = 5 (substituting the value 2 for the 𝑦 in the equation)
𝑥 +2 – 2 = 5 – 2 (do the same thing to both sides)
𝑥 = 3
The 𝑥 and 𝑦 are variables that can have values assigned to them for the sake of solving the equation. In a computer program, the value of a variable can be — and usually is — changed as the program runs.
2. Naming a variable
We will need more than just 𝑥, 𝑦 and 𝑧 to write our programmes, but more importantly, we will need to be able to keep track of our variables and what values they are being used to represent. For this reason, we use variable names and not just letters.
If we are costing items in a shop, at some stage we will need to calculate the VAT amount. Rather than using and 𝑥
, 𝑦
or 𝑧
for this variable name, we can use something like vatRate
as our variable name.
This means the difference between code that ends up looking something like the following:
salesPrice = productPrice × vatRate
instead of something like:
𝑥 = 𝑦 × 𝑧
Naming conventions for variables vary from language to language.
repeat 4 [ fd 100 rt 90 ]
3. Declaring a variable
Instead of coding the number of steps to move forward and the angle to turn into the program, we will assign values to variables and then use the variables in our program. At first, this appears to have little benefit, so you will have to be patient for now and trust the process.
Test your code at every step!
The make
command creates a variable as follows: make "nameOfVariable value
. In the below code, we create 2 variables and assign integer values to each (note the quotation mark before each variable name):
make "numberOfSides 4 make "lengthOfSides 100
Visit our LOGO cheatsheet here!
4. Using a variable
Substitute the values in the existing 2 lines of code with the variables created above (note the colon before each variable name):
repeat :numberOfSides [ fd :lengthOfSides rt 360/:numberOfSides ]
And together:
make "numberOfSides 4 make "lengthOfSides 100 repeat :numberOfSides [ fd :lengthOfSides rt 360/:numberOfSides ]
5. Next steps
Head on over to the tutorial Get some user input in LOGO to learn how to get the most out of your variables!
References:
- Rouse, M. (2022). Refactoring Techopedia. Available at: https://www.techopedia.com/definition/3865/refactoring (Accessed: 27 September 2023).
- AG, C. (2023). Find all Unicode Characters from Hieroglyphs to Dingbats – Unicode Compart. Available at: https://www.compart.com/en/unicode/ (Accessed: 25 January 2023)
- Turtle Academy (2023). Lessons. Available at: https://turtleacademy.com/lessons/7 (Accessed: 28 January 2023)
- Terrapin. (No date). Commands Overview. Available at: https://resources.terrapinlogo.com/weblogo/commands/ (Accessed: 22 April 2024).