Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jaredpalmer
Forked from koenbok/advanced.md
Created June 15, 2016 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredpalmer/3d31548792b69f9019f017eec730e0c9 to your computer and use it in GitHub Desktop.
Save jaredpalmer/3d31548792b69f9019f017eec730e0c9 to your computer and use it in GitHub Desktop.
Learn Programming

Advanced Programming

Programming setup

  • Instant visual output.
  • Fast startup time, fast execution.
  • Sensible errors, with code locations.
  • Easy to set up and get working.

Architecture

  • Don't repeat yourself, keep it simple, don't over design.
  • Naming things is hard, but important.
  • How to write comments for others and yourself.
  • Isolation and determinism.
  • Program and state.
  • Dealing with errors.

Debugging

  • Print statements are fine.
  • Printing output is an art.
  • Breakpoints, step in, step out.

Scoping

  • When does code have access to which variables.
  • Global versus local scope.
  • Function versus block scope.

Object oriented

  • Platonic realism. Classes and instances.
  • Constructors, or how to create a new object.
  • Values versus objects, equality.
  • Reusability, overrides.
  • Internal object state.
  • Private and public methods.

Functional

  • Functions as code containers, just like files. With inputs and outputs.
  • Named versus anonymous functions.
  • Composing functions.
  • Pure functions.
  • Curry-ing, memoization.

Concurrency

  • The art of doing multiple things at the same time. At what level? CPU (cores), computers, networks.
  • Sequential and parallel.
  • Why is it so hard to reason about concurrency. Invoice numbers and checkout example.
  • Consistency for the state of your program. Like counter example.
  • Locking as a problem and solution.
  • A different approach: single threaded event loops. Not all tasks are equal.

Async and callbacks

  • Example with playing a sound; wait or continue?
playSound() # 0.5 seconds
print "Hello"
  • What happens if you wait? Stop all execution.
  • What happens if you continue? How to do something after the sound played? Delay?
  • How would you wrap code that you don't immediately want to execute?
  • Apply to the concurrency problem.
  • Dealing with async errors.
  • New approaches, promises, await.

Testing

  • Write tests before you build.
  • What can and should you test?
  • Always test everything, code coverage.

Programming

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 = 33
years = 10

print 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 + 100

print x(100)
print x(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"
else
    print "His name is not Koen"

Loops

For items in structures you often want to do something to every item in a list.

for item in people
    print item

Tips for teaching

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment