Welcome back to another edition of {Kids: Code} Weekly! This week we’re going to take a look at debugging, a topic we talked a little bit about last week. Later on, we have some great new lessons to stretch your Scratch creative limits and test your knowledge about AI. All that and more below!
Want to reach out to us? You can contact us here or check us out on Twitter!
Last week, we took a look at Swift Playgrounds - a new and fun way to code in Swift for iPads, iPhones, and Macs. One of the things we talked about briefly was "debugging", or how we could use the language and editor to better understand when something is wrong. This week, we'll take a quick look at some debugging patterns you can use in other languages. Debugging is a skill that can serve you well both inside and outside of coding, so give it a shot!
Android? Eagle eyed readers may note that we were originally planning to run an Android tutorial that's similar to Swift Playgrounds. Unfortunately, scheduling issues over the past week prevented us from getting to it - but it's still on our list to do, don't worry!
Print Debugging
The first trick we'll go over may sound obvious to some readers, but it's a useful trick that even veteran coders will use: just print something to your screen with each step!
For example, let's say we've got a loop that we want to better understand: we know it's going to have some values, but we may not know what exactly. This is where our print statement comes in handy:
Python:
values = [1, 2, 3, 4, 5]
for value in values:
# Multiply by 10
value = value * 10
# We want to know when we hit 3, 4, and 5 - so check if
we're past 30, since
# we're multiplying by 10 above
if value >= 30:
print('Past 30!')
Here, we put our print
function behind an if
loop - but you can put as many print statements in your code as you need to debug!
JavaScript If you're writing JavaScript, the common print mechanism in that language is
console.log()
.
Breakpoints
Many editors and coding environments support another type of debugging, known as breakpoints. A breakpoint is often set one of two ways:
In your editor, by clicking somewhere associated with a particular line of code. In the screenshot below, you can see that we've added a breakpoint to our line by the blue triangle marker.
In some code (like JavaScript), you can write a special command in your code (
debugger
), which will pause the code when it's running there.
In both cases, you get to see your code as it's running. Your browser is capable of this too! If you open an Inspector (right click -> inspect element on any page), and navigate to the Sources tab, you can set breakpoints in most JavaScript files. Give it a shot and reload a page, and see how the code pauses there to inspect further. You can run this with your own code, too!
Some folks at Harvard have put together a special series of lessons for those Scratch users who want to really flex their creative muscles. With “Getting Unstuck” you’ll be going through 10 modules that help you when you hit writer’s block with your code where you just don’t know what to make. You can check out the video above to get an idea of what it’s about - and if you’re still interested, you can get started by going here!
Introducing artificial intelligence and machine learning from Code.org!
Recently, Code.org has put together a brand new module that teaches artificial intelligence (AI) and machine learning (ML) for middle school students! These lessons teach you about how these technologies are important to our world and how they are able to identify and help solve complex problems that are all around us.
You can read more about this new lesson plan here, and if you’re ready you can check out the module for yourself by clicking here.
For the Parents
Back in June, a great article (which you can read here) was submitted to the Journal of the Data Visualization Society that highlighted the author’s experiments with helping her child visualize data with practical, everyday items. Using animal toys, colored candy, and more, she had her son group them based on different categories and map them out accordingly.
In a way, many of the lessons we’ve been featuring in {Kids: Code} deal with data visualization. For example, when we talk about “arrays” and “sorting” we’re talking about certain ways to organize a list of items we have. Sometimes when it comes to wrapping your head around a tough computer science problem, finding a different way to approach it is the key!
This Week’s Vocabulary Word
Debugging
The art of reasoning about why a chunk of code is executing the way it is.