Rik explains…Debugging your code

Code Club’s Senior Content and Curriculum Manager, Rik Cross, is not only in charge of creating the amazing projects in our curriculum – he also runs a Code Club in his local school in Leeds. His brain is full of amazing knowledge that we thought we ought to tap into more regularly, so we’ve started a blog series, which we’ve dubbed “Rik explains”. This blog tackles a common issue a lot of volunteers face in Code Clubs: fixing bugs in Scratch…


Something I sometimes see in my own Code Club is children putting up their hand and saying “My code doesn’t work” and sitting back and expecting me to fix it!

image01

Often, it can be a lot of code, developed over a few weeks. As I’ve not written the code myself, initially I don’t know how to fix their code any more than they do. As I’m helping children to fix their code, I try to share my thought process with them, in the hope that I can empower children to fix their own code, especially at times when I may not be on hand to help them.

Here are some practical techniques that can be used to debug Scratch code….

1) Code is usually read many more times than it is written. It is helpful to comment scripts with their intended purpose, as well as commenting blocks (or sets of blocks) for which the effects aren’t immediately obvious.

2) As projects get larger, it can often be frustrating to run an entire script just to check whether the last few newly added blocks work as expected. Groups of blocks can be separated from their containing script and clicked, allowing them to be tested in isolation. They can then be dragged back into the main script once they have been tested.

image03

3) It helps to make script output as visible as possible. Variables can be displayed by checking the tickbox next to the variable name. The variable name will then be displayed on the stage, along with the current value. You can also do the same with some sprite and project properties

image09

‘Say’ blocks can also be used to make code output more visible, and individual blocks can be tested by simply clicking them — they don’t need to be attached to a script in order to be run!

image02

 


4) Slowly stepping through scripts can be achieved through the use of ‘wait’ or ‘wait until key pressed’ blocks, slowing down the execution of a complex or fast-moving code;

image00

5) Testing expected and unexpected user input can lead to making a script more robust. For example, take a look at the following script:

image08

What would happen if we ran the above code and answered “‘yeah” or “yep”? We could make the script above more robust by allowing for more than one positive answer:

screen-shot-2017-02-13-at-14-43-51

You could even achieve this by using a list of accepted answers:

screen-shot-2017-02-13-at-14-43-57

6) One final technique I want to mention is the use of cheats as a testing method. Although children often see cheats as a way of making playing a game easier, they were first introduced to make play testing easier. Before a game is released, the entire game has to be rigorously tested, and cheats make this easier. For example. How can you test the last level of a game without having to repeatedly play previous levels? A good cheat shouldn’t be easily discoverable, or interfere with normal play. Combinations of key presses work well, but children also really enjoy creating tiny 1-pixel sprites that can be clicked to activate a cheat or other ‘Easter egg.

image05

Sharing techniques with children to spot and fix problems in their Scratch projects themselves will empower them, giving them more ownership of their creations. This is especially important for lifelong learning, at times in the future where there may not be an educator on hand to help them. In fact, many of the strategies covered in this workshop are used in industry by software development teams!

Code Club films: start a club in your school

What do Tile Hill Library, Y Bont Faen Primary School, and Liverpool Central Library all have in common? Answer: they are all Star Clubs who agreed to let us film their club sessions last term!

We wanted to make some films that showed different Code Clubs in action, to help us spread the word about our programme and encourage more schools and libraries to start up clubs.

The week before Christmas we jumped in a car packed full of equipment and headed across Wales to visit Y Bont Faen Primary. Despite the fact that they were all a bit tired after their Christmas show, the children (and adults) were very energetic and helped us create a great film about how Code Club works in a school environment.

Watch the film here:

We’ll be releasing two more films in the coming weeks, so stay tuned! In the mean time, you can check out more great content on the Raspberry Pi Foundation Youtube account.

Projects for Safer Internet Day

Tomorrow is Safer Internet Day, which aims to promote the safe use of digital technology for children and young people. The day offers the opportunity to highlight positive uses of technology, and to explore the role we all play in helping to create a better and safer online community.

At the Raspberry Pi Foundation and Code Club, we’re committed to helping people to learn more about digital making, building a strong and supportive community who use their skills in positive, fun and creative ways.

To celebrate Safer Internet Day in your school, community centre, Code Club, or at home, we’ve created some fun projects that promote the safe and responsible use of technology.

Username Generator

image02

There are lots of websites and apps that identify you by a username. This username is often visible to others, so it’s important that your username isn’t your real name, and doesn’t include personal information such as your age, year of birth or where you live.

In this project you’ll generate usernames that you can use on websites like Scratch. You’ll be able to save the usernames that you like to a text file, so that you can use them later. You’ll even have the chance to create a profile picture to go with your new username.

Password Generator

image01

In this project, you’ll learn how to generate random, secure passwords. First, you’ll learn how secure your passwords are, as well as what makes a secure password. You’ll then create a program to generate random passwords, allowing the user to decide how many passwords they want and how long the passwords should be.

Secret Agent Chat

image00

Our personal data is important and should be kept safe and stored securely if shared online. Many websites use encryption to keep the data they hold private. This project shows you how to use a basic encryption technique, and how unauthorised people may be able to gain access to your encrypted information if you are not careful!

Find out more about Safer Internet Day on their website, which also contains education packs for learners, parents and carers.