You understand that, right?

This is the second part of a series of articles called Learn to Code. Click here to go to Chapter 0, “Wax on, Wax off”.

 

Learn to Code

Well done. You’re now a World class programmer! And you’ve been given a brief  by your customer who wants you to write some software for them. The following sentence is the first line of their brief to you:

“Bob is a man. He is 30 years old. He can run 5 miles.”

What a peculiar start!! But bear with me. Remember, in order to start programming, we have to fully understand the real-world thing we are transforming into some computer code. Let’s take that sentence apart and write some bullet points about what we know about Bob:

  • Bob is a “something”.  A real thing. A proper object.
  • That thing is categorised as a “man” (whatever that is).
  • Bob has something called an “age”. The value of his “age” thing is “30”.
  • Bob can perform an action known as “running”.
  • Bob can do that action for “5 miles”.

 

Sorry for all the quotation marks and playing-dumb, but all will become clear as we move through this lesson.

Like pretty much anything in the universe, Bob can be defined by a set of “properties” (like his age, height, inside leg measurement) and the actions that he can perform (like “run”). In proper programming, we call these “properties”, “properties” (hahaha, surprise! I slipped a programming term into your vocabulary!). Slightly more complex, we call the actions he is capable of performing, “methods” or “functions”, depending on which programming “language” you’re using. We’ll use the term “functions” to start with because the first programming language you’ll use demands this terminology.

The programming language we’ll start with also prefers things to start with a lowercase letter. For now, just accept that, we’ll cover why later. Let’s start by creating Bob in code. 

bob

 

You don’t need one of these to get at Bob’s information. All you need is a “dot”. True story.

What is Bob Made Of?

To “access” information (properties) or to perform actions (functions) you need to use an “accessor”. The different programming languages use different accessors. But the most common one you find is the “dot” (“full stop” to you Brits, or “period”, to you Americans).

For example, to “access” Bob’s age, you would write:

bob.age

 

The “dot” tells the computer that it’s Bob’s age you care about and not somebody else’s. Consider the following:

bob.age

betty.age

earth.age

 

The “accessor” – the dot – makes it very clear exactly what you’re asking for, and where from.

Each line in the code above is an example of an “expression” because each results in (or “returns” as we programmers say) some information. You could then use this information somehow, either by showing it to the user, or maybe using it in another expression:

bob.age + bob.age

 

If Bob’s age is 30, this expression “returns” the number 60 (but remember, it doesn’t change Bob’s age, it simply uses it as part of a calculation).

IMPORTANT NOTE: Now before we go any further, it is important to know you need to be able to tell the computer that runs your “program” when you’ve stopped doing one thing and started to do something else. It’s like finishing a sentence with a full stop. And then starting another sentence.  For this, we need to put a semi colon “;” on the end of some of the lines of code.

We haven’t actually “set” Bob’s age property yet. To do that, we must use the equals sign as discussed in the previous chapter. So let’s see what a proper line of code looks like:

bob.age = 30;

 

If you’re a proper programmer, that looks like a line of code you’d see everywhere, every day.

 

steve.bellow();

Performing Actions

To make Bob perform an action you simply use the “dot” accessor again to show you’re doing something with Bob, and then you write the name of the function (in this case “run”) you want to perform. Then you do something a little strange, you write an open-bracket “(” then a close bracket “)”. Like this:

bob.run();

 

Strange isn’t it. It might make more sense if I explain what else you can do with those brackets: you can put information inside them so that the function behaves differently each time. For example:

bob.run(5);

This might make Bob run 5 miles. Putting 10 in the brackets might make him run 10 miles. It all depends how that “function” is written.

In the next chapter we’ll do just that…