Lesson 3 Methods

So far, with what I have told you, you could code pretty much anything. You have been given the most basic of the building blocks and, if you were so inclined, could run off into the night like a man with a hammer that sees the entire world as a nail. You could, in theory, also use Lego bricks to build a sky scraper. Doesn’t mean you should.

Have you ever spoken to someone and explained a long and possibly laborious task? If they’re particularly difficult and slow, you may find yourself gritting your teeth and thinking “I’ve told you this 5 times already.”

Imagine you’re teaching someone to drive a car. They’re a couple of weeks in and should be up to handling a right turn on a roundabout. But there’s a problem. They only understand basic pedal, gear and indicator instructions. Every time they need to start off, you need to talk them through how to pull away because saying “pull away” meant nothing to them.

Normally, you tell someone how to do something, give that task a name and ask them to perform that task. Taking out the rubbish to give a simple example; you tie up the rubbish bag, remove it from the bin and take it out to be collected. The same goes for the driving student. They should know what “pull away” entails automatically. You’ve already told them how to do it. The flow diagram below should illustrate my point. For goodness sake, don’t use this as a guide to driving! I’m sure a lot of instructors reading this would tear into it.

Driving flow diagram

Turning at a roundabout should start with pulling away, at some point. I’ve been quite simplistic but I have tried to break down the implicit instructions when saying “pull away”.

C#, like most languages, gives you a way of saving repeating yourself. They’re called methods.