You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Learning how to program is hard. This is my attempt to make it easier to teach for myself. I often use this document as a base for a presentation, but it's most important to let people do the exercises themselves and learn from it. All the code is meant to be ran in Framer Studio and uses CoffeeScript.
Programming is hard because
It is a mix between English, logic and math.
It is very strict: tabs, spaces, commas and capitalization matters.
Errors can be hard to understand but often mean something simple.
Before we begin
You can display results with print 123.
Errors appear in the bottom and you can dive deeper with inspector.
Values and types
Numbers: 10, 0.0283, precision.
Strings: "a", "Koen is nice.", "412" or " ".
Booleans: true or false, or actually just 1, 0.
There are more subtypes like booleans, but these will do for now.
Operators
Things that change values, like math: +-/*.
print 10 + 10
print 10 / 10
print "a" + "b"
print "a" + 10, pretty nonsensical, right? It changes your value type.
Variables
Variables is just assigning a name to a value so you can use it anywhere and as often as you like. It's like x = 10 in math, yet in programming you may use full words (without spaces or special characters) but keep them simple like: name = "Koen". If you need multiple words you'll often see things like personName = "Koen".
Let's write a program that displays my name and age ten years from now.
print"Koen will be "+ (33+10) +" in 10 years from now."
Notice we use 10 twice. Let's convert this into a program we can use for anyone by using variables.
name="Koen"age=33years=10print name +" will be "+ (age + years) +" in "+ years +" from now."
Change the variables and see different output.
Structures: Array and Dictionary
Something we pretty commonly do is making lists of things. These are called Arrays in programming. Let's invent them.
1. Koen
2. Jorn
3. Sara
What could you do to a list (add, remove).
How would you add an item to this list?
How would you remove an item from this list? Aha, you need its number. We call these an index in programming.
How would you get an item from the list. Yep you need the same number.
Ok. Now let's actually do it. Create an Array like:
people= ["Koen", "Jorn", "Sara"]
Read an item from the list with an index print people[1]. Why would you get this result? Computers count from 0.
Add an item with push like people.push("Ben").
Delete an item with delete like delete people[0].
Also note you can say things about the list in aggregate. Like the length or you can re-order it because lists are sorted.
Now, we will invent a dictionary. They basically only have one difference, let's see if you can figure it out. Assume you have a list of first and last names, like above, and you need to look things up by last names. What would that change?
Indeed, you'll want to use the last name as an index (or a string instead of a number, right?). We call these keys. The notation is as follows:
people= {
bok:"Koen",
dijk:"Jorn",
suhr:"Sara"
}
How would you read an item? people["bok"]
How would you delete an item? delete people["bok"]
Input and output
Before we dive in deep with functions let's take a step back. In computers we talk a lot about I/O, or input output. Input can be anything but is most often things like a keyboard. Output can be a screen or a printer. Can you think of anything more?
But I/O doesn't just work on that level, it works on every level. For example your hard drive also provides I/O, just like your network. So basically putting things in and getting stuff out is all that computers are about.
Function
At the smallest level, a mathematic formula has inputs and outputs too, can you discover them?
x = y + 100
A function is the exact same thing. But the notation is as follows. Again, can you spot the inputs and outputs.
x= (y) ->return y +100printx(100)
printx(20)
Notice how you can add as much code as you like in the function body, and that you can re-use it as often as you want. Now, as an exercise let's put our name and age calculation program in a function. Given the following, can you finish the function body and use it for a few different values?
program= (name, age, years) ->return …
Congratulations, you just wrote your very first computer program.
Logic
Logic allows you to only do things on certain condition. It uses the if and else keywords.
if name =="Koen"print"His name is Koen"elseprint"His name is not Koen"
Loops
For items in structures you often want to do something to every item in a list.
The main driver for someone to learn something new is motivation. A great way to motivate people is to make them see how they can apply what they learn to solve problems. It can also help to relate examples and analogies to their background.
One of the biggest de-motivators can be intimidation. If something looks too hard or complex, it feels like it's not worth putting time into. Things like programming and math (but also Chinese) tend to have this effect on people. So keep your examples and exercises on level, and slowly work your way up.
My best teaching trick by far is to let people invent things by themselves. Many programming concepts are easy to come up with yourself if the problem is laid out properly. Try to make thought exercises that slowly work towards the solution for a problem like how to deal with lists of things (an Array).
It's hard to see what is hard for teachers, because it seems so obvious. My favorite example is our snippet layer = new Layer which makes total sense to programmers. But now imagine that you are not paying attention to capitalization, and definitely don't know that a capital means a constructor.
Repetition is very important to become good enough to do things by yourself. Make sure you summarize learnings at each step and do multiple exercises for the same concepts with time in between.
A great trick to see if people understand a concept fully is to let them explain it in their own words to the rest of the class. Take into account social pressures that can harm the quality of the answers, like too many unknown people in a group.
Some of the words in programming are very well chosen, but can be hard to grasp for non native speakers. For example a graph often means nodes with connections, but Dutch people tend to think about charts when they hear it.