One of the most important things when introducing anyone to programming is having them understand core concepts that will crop up over and over again. This isn’t as easy as you’d initially think — while existing developers have no challenges with the logical thought process around concepts we’ve been using for extended periods of time, the idea of a variable, a function, or something more complex, may not be terribly easy to grasp for others.
Just remember that everyone comes at challenges with different backgrounds, different levels of existing skill and different ways which they learn best. I love getting people into technology, and I use the same few analogies frequently to explain core concepts. I wanted to write them down in a post not only to share them, but also to get feedback and ask for any contributions.
Imagine you have a box of all of your football kit — from your favourite team. I’m not a football fan, but let’s just say Arsenal are my team of choice. I get a sticky label and mark the outside of the box “Football stuff”. When I want someone to grab my stuff, I can just tell them to look out for the box labelled “Football stuff” without having to go through the con-tents.
The interesting thing is, that the contents of the box can change, for example I may change the team I support to Manchester United, but the name of the box will stay the same. This is known as ‘reassigning’ variables.
An array is very simply a list of data — that data could be anything — words or phrases (strings), number values (numbers/integers) or true/false values (booleans). All that matters is that there’s something which relates the items in the list. It could be a collection of comments, favourite music tracks, approvals for coursework, or anything else.
Functions are just a set of instructions you’ll need to repeat — like making a sandwich. A sandwich is just a set up steps — get one slice of bread, butter the bread, place down a slice of cheese, and so on. If our application needed us to do this frequently, we could store the list in a function, and instead of writing out every instruction, we could just say “I’m making a sandwich”.
Functions with parameters or arguments
While most of the process in making sandwiches is the same, there are often some differences in how it is put together — for example the filling. We could write most of the instructions, but note that when we are making a sandwich, we’ll have to specify the filling.
“I’d like a sandwich with cheese.”
“I’d like a sandwich with egg mayo.”
Sandwich is our function, our base set of rules, and the filling is our parameter or argument.
However much we may not think of it as a fundamental part of life, the basic “if/else” conditional powers most of our decision in day-to-day life.
“If it is sunny, then wear shorts.”
“If I have time, have a bath. Otherwise I’ll have a shower.”
This is incredibly powerful in programming for doing certain things if our conditions are met, and providing alternative instructions if not.
You have a pile of paperwork in your office, and you have to do the same few things to all of them:
- Check the ID number of the report
- Check the information is correct
- Stamp the paper
- Put it in the completed pile
No one would ever instruct you in what to do for each step for every item of the list — that’d make the list dozens, hundreds or sometimes thousands of times longer than it has to be. You are told once what to do with all of them, and to keep going until you are finished, or hit a certain number.
This is the basic flow of a loop — until something stops being true (there are items left in the list, you have done less than 100, etc), then you should stop. These are often combined with arrays to execute the same logic on each individual item on the list.
Libraries and Frameworks
Libraries are often a set of pre-written functions that other people think you’ll find useful and package up. They could do anything, but the important thing is that they can be added on top of your existing projects to make certain tasks easier.
Frameworks, on the other hand, are used at a much more base-level of your project, and generally dictate how you should structure and build your project. This often comes with the added benefit of familiarity if another person needs to inherit your code.
Okay, so let’s use the restaurant example I’ve seen used before. On entry you are given a menu — this describes all of the items you can ask for, including some variations on the dishes (for dietary requirements). This list of all the options is our API.
You make an API ‘call’ when you talk to your waiter and tell them what you want. All of the work magically happens without you having to intervene (hopefully) and the food gets delivered to you.
This is similar to how an API works. You request something (could be a request to upload a photo, or some data about a user), and assuming your call is made correctly, the system returns it’s response to you without exposing what’s going on in the background.
Most programs are build in three parts, and we’re going to use seeing a play in theatre to explain them!
1. Statements to establish the start of the program
This is like the set of the play. It sets the scene, helps you set context and explains the constraints that the actors are working in.
2. Variable declaration
These are your opening few scenes where you understand what we’re dealing with in terms of characters, where they are, their goals, and the basic story set up.
3. Program statements (blocks of code)
This is the clever stuff that makes up the story — the relationships, the conversations and how their outcomes affect everything else, and how we’re doing with the goals.
Share your thoughts!
I’d love to have a conversation and possibly update this post with more examples from other people! If you have better examples for some of these concepts, please also share them!