GoalDebug a program by comparing it with its algorithm, and say what extensions and text-based languages are.
New words
debug · to find a bug and fix itextension · a set of extra blocks that can be added to Scratch, such as Pen or Musicblock-based language · a language where we build programs by snapping blocks togethertext-based language · a language where we type the instructions with words and symbols
Explanation
To debug a program, compare the code with the algorithm, one step at a time. Look for four common bugs: a step in the wrong order, a wrong number, a wrong word or variable name, and a script on the wrong sprite. Then trace the program, change one thing and test again. Scratch can also be extended. An extension adds a new group of blocks. Pen adds drawing blocks. Music adds sound blocks. Text to Speech adds a block that reads words aloud. Scratch is a block-based language. Other languages are text-based, and in them we type instructions. Both kinds use the same ideas: sequences, loops, choices and variables.
Bug
Example
Fix
wrong order
hide comes before show
swap the two blocks
wrong number
move 200 instead of 20
change the number
wrong name
a different variable is selected
select the right variable
wrong sprite
the script is on another sprite
drag it to the right sprite
Worked examples
The algorithm says: show, say ‘Welcome!’ for 2 seconds, hide. The code has hide first and show last. The sprite hides and then talks while hidden, and at the end it shows. Swapping the first and last blocks fixes it.
An extension adds blocks. After the Text to Speech extension is added, a sprite can read a message aloud. Extensions are added with the button in the bottom-left corner of Scratch, and a grown-up should help.
In a block-based language we drag blocks, like this repeat. In a text-based language the same repeat would be typed as words and symbols. The idea is the same.
Class activity
Bug hunt (pairs). Each player writes an algorithm of four steps for a sprite, for example ‘show, say hello, wait, hide’. Then each player writes a ‘code’ version on paper with exactly one bug: a step in the wrong order, a wrong number or a wrong word. Swap papers. Compare the code with the algorithm, step by step, and find the bug. Say which of the four types of bugs it is. How did you find it?
Practice
1
The algorithm says ‘Move 20 steps’ and the code says ‘move (200) steps’. What type of bug is this? How do you fix it?
It is a wrong number. Change 200 to 20.
2
Say what type of bug each is: (a) ‘hide’ is placed before ‘show’, (b) the script is on the wrong sprite, (c) the variable ‘score’ is chosen but ‘points’ was meant.
(a) wrong order. (b) wrong sprite. (c) wrong name (the wrong variable).
3
Give two differences between a block-based language and a text-based language.
In a block-based language we snap blocks together, and the blocks show the choices. In a text-based language we type words and symbols, so we must spell everything exactly. Both use ideas such as loops and variables.
4
What is an extension in Scratch? Name one and say what it adds.
It is a group of extra blocks. For example, the Pen extension adds blocks for drawing lines, and the Music extension adds blocks for notes and drums.