So you want to learn to code? Well this is the right place. I assume no prior knowledge, except knowing how to turn on a computer, open a text editor such as Notepad, and have some idea of the operation of a keyboard. It may not be immediately obvious WHY I’m covering some of the topics in this article, but rest assured that, like Daniel-san painting fences or waxing Myagi’s car, it is all about preparing your state of mind.
Actually, while we’re on the subject of films, before you begin, make sure you watch The Social Network and The Pirates of Silicon Valley. They’re essential viewing for wannabe programmers!
Basic Mathematics
WAIT! Stop! Don’t be afraid, we’re only going to touch the absolute basics. Primary school stuff. Nothing else. That’s all you need!
Really? I hear you ask. Yes!! You don’t need to be a maths whiz to be a great programmer. Leave all that business to the computer!!
If you’ve ever written this:
1 + 1
…you’ve written a “program”. A program is simply a set of “instructions” for the computer to perform. Even if it’s just 1 + 1! Sure, most programs are a lot more complicated than adding 1 to 1, but at its most basic, that’s it. Easy!
Computer programming (also known as “coding”) is usually done to replace human effort with automation. The first step in any coding project is usually to break the real world problem down to its smallest parts. That way you can better understand what it really is that you’re turning into code.
Lets break down our simple addition program above to its raw components:
- Something on the left hand side on the plus sign.
- Something on the right hand side of the plus sign.
- The plus sign.
Thanks to great educational systems across the world, you instinctively know what to do with a “+” sign. You take whatever is on the left and add the thing on the right to it. So the purpose of this program is to take 1 and add 1 to it. Simples. And you’ve just read your first piece of code.
Now, consider the following “program”:
Bob's age + 1
You take Bob’s age (let’s say 30) and add 1 to it (giving you 31). But does Bob’s age actually change by running the above program?
No, it doesn’t. The “result” will be the number 31, but the two parts of the sum aren’t affected. In same way that 1 + 1 equals 2, the two 1s don’t actually change their value. If you have two £1 coins, you have £2 in total, but you haven’t magically transformed the two coins into one single £2 have you?
And there are lots of different ways of getting to £2. You could have four 50-pence pieces. Or ten 20-pence pieces. They all “equal” £2.
Why am I banging the “equals” drum? Because in programming we care a lot about the “result” of calculations. Once you get your head around that you’re well on your way to understanding the concept of something in programming called “expressions” (you’ll find that in programming a lot of things have names that are really more complicated than the thing they represent…). “Expressions” are just different ways of representing or getting some kind of information. Remember back to fractions in school. 1/2 and 2/4 and 4/8 are actually all the same amount. In coding there are different ways of “expressing” the same information, only sometimes that expression will involve a calculation or getting a piece of the puzzle from somewhere else like another computer or web site.
Here are some examples of expressions:
- 1 + 1
- 25 – 3
- Bob’s age + 1
- Bob’s age – Betty’s age
- (Total Payroll Expense) / (Total Employees)
On that last one, we use the forward-slash to mean “divide” because not many keyboards have a proper divide symbol on them! That example is an “expression” that gives you the average wage in your company: total cost of employee salaries divided by number of employees.
Don’t be concerned by details such as using the forward slash as a divide symbol because you’ve used forward slash as a divide symbol all your life. Just look at those fractions a couple of paragraphs up. 1/2 and 2/4 and 4/8. Fractions like this mean take the thing on the left and divide it by the thing on the right. Nothing new to see here.
The final piece of the mathematics puzzle is “equals”. Equals is used to change the thing on the left to the result of the expression on the right.
So the way we add 1 to Bob’s age is to use the “equals” sign like this:
Bob's age = Bob's age + 1
Well done, you’ve reached the end of this lesson and, although you might not believe me, you’ve got the basics of programming already setting roots inside your brain. I’ll prove it to you soon as we start writing some code in the next few chapters…
Next Chapter: Meet Bob or see the entire series.