9_3_5

WHAT: Evaluate the purpose of meaningful identifier names and simplifying programming structures. E.g. While play: instead of While play = True

HOW:

Activity 1 - READ

Here are two variables:

  1. sidVariable
  2. choice

Try to think about what might be stored in those variables.

Which one is easier to try and guess?

Variables are like saving files on your computer. If you don't give them a sensible name then you will soon forget what they are for and how they should be used.

If I wanted to create a variable that stored the current number that I was trying to guess, which would be the best option for me to use?

  1. house
  2. guess
  3. x
  4. variable

I am really hoping that you picked guess!

Activity 2 - READ

The closer your code is to English, the more elegant it will look. It will also make it much easier for other programmers to follow and for you to understand it after not looking at it for a few weeks.

It is very quick and simple to write this code for a password checker:

password = "four"
checkpass = input("Password :")
while password != checkpass:
     print ("invalid")
     checkpass = input("Password :")
print ("granted")

However, a more elegant way to write it is like this:

password = "four"
checkpass = input("Password :")
passwordIncorrect = (password != checkpass)
while passwordIncorrect:
     print ("invalid")
     checkpass = input("Password :")
     passwordIncorrect = (password != checkpass)
print ("granted")

If you look at the while loop now you know exactly what it is doing. It is saying, while the password is incorrect, keep asking them for the password. For the first example you have to find password and see what is stored in it and then find checkpass to see what is in it before you can figure out what the loop has been created for.

Now this is a very simple piece of code but when your programs become larger; useful identifiers and elegant code become vital.

CHECK:

EMBED:

Look back over code that you have created in the last few outcomes.

Have you used meaningful identifier names?

Evaluate how elegant your code is and make improvements where necessary.

CLASSROOM IDEAS:

I would recommend lots of formative assessment of student code during this session! Go round at look at how elegant their code is and give them tips. You could even employ some of your elegant coders to give advice to other students.