After a bit of teaching, students are great at writing code. They don't necessarily write the right code to solve the right problem, but never-the-less, they can competently write code. How many times have you see a hand pop up in your class and for your student to say,
"It doesn't work?"
And of course, what is the first thing you do? You start reading their code looking for the problem they have made so that you can show them how to fix is. So why can't students read their own code for themselves? Surely if you can write code you can read it? Not necessarily. From my own observations I believe there are a few things going on here.
We don't explicitly teach students how to read code as a thing in its own right. We teach coding syntax and assume that if students can write code they can also read it.
Students don't know that the first step of fixing bugs and problems is to read their code and compare what they thought it should do with what it actually does.
Written code is often back-to-front compared to English and students don't have a way to interpret and translate their code efficiently.
Coding uses words students don't use in their every day speaking like 'while' and 'else'. As a result, they don't mean anything in code.
To tackle this issue, let us look at some key concepts of coding and provide some English translation which students should be encouraged to say out loud when they are discussing their code.
Variables
We know that a variable stores a value so when talking about a variable, we should refer to "the value stored in <variable name>" when talking about them. In strongly typed languages variables have a data type and it is useful to refer to this as type is as well. Here are some examples of declarations and syntax and their English equivalent.
int a = 30; // The integer variable a is declared and the value stored in
// a is 30
a = b + c; // the value stored in b is added to the value stored in c.
// The answer is copied into a
Console.WriteLine(a); // Write the value of a on the console
Selection
Selection (IF) is easy to an extent, but the 'else' clause often trips up young people who are unlikely to use this word in everyday language. Thankfully, substituting it for 'otherwise' solves this problem. I like to think of the phrase "If it is true" to expand the meaning of if since 'if' only operates on clauses that are true. Here is an example of selection with English translation underneath
if (age >= 17)
{
Console.WriteLine("You can drive");
}
else
{
Console.WriteLine("On the bus young 'un");
}
If it is true that the value stored in age is greater than or equal to 17 then
Output "You can drive" to the console
Otherwise
Output "On the bus young 'un" to the console
WHILE
This is perhaps the hardest of all the constructs. Students struggle with the idea that the code loops back and also when to use this construct. An example of when you might use this at GCSE and A-level are primarily to have the program wait for the correct input to be entered. The translation that works is "As long as it is true that..." Here is an example. Note that writing line numbers helps in this case.
1 Console.WriteLine("Enter secret password: ");
2 string password = Console.ReadLine();
3 while (password != "12345")
3.5 {
4. Console.WriteLine("Try again. Enter secret password: ");
5 string password = Console.ReadLine();
6 }
7 Console.WriteLine("Access granted");
Write "Enter secret password: " to the console.
Read the input from the user and copy the value into the string variable password
As long as it is true that the value of password is not equal to "12345" then (if it is false, jump to line 7.
Write "Try again. Enter secret password: " to the console.
Read the input from the user and copy the value into the string variable password
Loop back to line 3
Output "Access granted" to the console
In summary
It is very important that we provide our students with a mechanism for decoding code and translating it into English so that it makes sense. I have detailed a few constructs and ideas that can be used above. Feel free to adapt these to your own contexts and extend them as you feel fit.
Update
I have created a helpsheet you can share with your students here.
コメント