Writing software is not much different from writing a set of instructions for a spouse, friend, or employee so that they can run your errands for you. If you have a lot for that person to do, a set of instructions might look like this:
- Go to the grocery store.
- Add milk to the shopping cart.
- Add bread to the shopping cart.
- Pay for the cart of items.
- Place the cart of items in the car.
- Bring the items home.
These instructions are simple. Of course, the computer can’t go to the grocery store, add milk or bread to a cart, pay for items, or do any of the rest of those things; Those things are all very complex in their own right. But we could write very specific instructions for each of those items in the list to the point where a computer, perhaps built into a robot, could do those things. The real difficulty in writing software is in figuring out what to write for all of the detailed instructions, not in writing the individual instructions themselves.
Writing a set of instructions for a friend to follow is sort of a metaphor for writing software. Sometimes programmers write their software as instructions like this to explain things to others or to help them see what is missing from the list; These text instructions are sometimes called pseudo-code since they are not written in any real programming language and have a very flexible structure.
Let’s look at another set of instructions for a human. These instructions are for a human who is rather dumb and has trouble figuring things out on their own:
- Open the list with the names on it.
- Pick the name at the top of the list that is not crossed off and continue following the instructions with that name in mind.
- Drive to the house where the named person lives.
- Cross the named person off the list.
- If the named person is home, put them in the car.
- If the named person is not home, go back to instruction 2.
- If the car is full, drive the car to our house and drop off all of the people in the car.
- If there are any names on the list not crossed off, go back to instruction 2.
This is how you will write software. You will write a bunch of instructions telling the computer exactly what to do. These instructions have a loop in them and the loop works just like a racetrack, string with the ends tied together, or any other item that, when followed, gets you back to the start point without ever ending. And like loops in computer software, there is a way to get out of the loop just like there is an exit on a racetrack so the drivers can get their cars back to the garage once the race is over.
Now, let’s combine the instructions from the first set and the second set into a single set of instructions and see how that works:
- Do the instructions in the first set.
- Do the instructions in the second set.
- Come to the party.
That was sort of easy. Instead of writing one giant list of instructions, I simply gave instructions that said to do the other instructions on the other two lists.
All of the things that are written in the instructions above are written like computer software. Like with computer software, the instructions are all made of smaller pieces and at some point, you won’t have to write more details. Also like computer software, there are things that will require a lot more detail. You can write instructions that reference other instructions too, just like in the above lists. And finally, you will always write software that can be terrible broke. The lists we wrote don’t tell the driver what to do if the car has a flat tire or if someone throws up in the car. There is no instruction to handle a situation where the milk and break cost more than the amount of money available. In real life, the person running these errands has set of instructions we don’t even know they have and they will follow those if weird things happen. In a catastrophe, they might just have an instruction in their head that says “panic and run away” but they still have that instruction. And the computer too has that sort of instruction; On a Windows computer, we have seen the results of panic as the infamous Blue Screen of Death.
You can see that writing software is both easy at times and complicated at times. You have seen simple instructions, instructions that loop over and over until finished, and instructions that reference other sets of instructions. These are some of the most common features found in computer programs and they are common regardless of the computer programming language you will be using.