What is a programmer?
The lure of creating a great mobile app or building your own website can captivate people to dig into the world of programming, only to be met with frustration because programming is different than most anything people have tried before.
Unlike your auto mechanic, who has something physical to work with like your engine, programming is more like art where the artist has a vision in their mind and with a stroke of a brush (well many strokes), bring that vision to life through a series of individual brush strokes, each one, while part of the whole, itself is an indistinguishable desecrate step to get there. Each stroke builds upon itself and the others to produce the final piece.
However programming takes that a step further. While the artist can see the brush strokes piling up on top of each other and see the vision start to form, programmers have to work with an abstract set of codes that themselves, resemble nothing in the final piece, but instruct the computer (or mobile device) do then draw the brush stroke. Imagine it like this…. The artist holds the brush, pallet of colors and has the canvas in front of them. You the programmer, without seeing the canvas at the moment instruct the artist to grab a dab of red paint, using a wide brush stroke, draw an arc from the lower left of the canvas, upward for 3 inches.
You repeat this process, with different instructions to the artist and then once you think you have enough on the canvas, you then take a peek. Return to where you can’t see the final product, instruct the artist on what need to be erased and done over. You repeat this process until the final pieces is done.
To make matters more challenging, that 3″ long red arc with a wide brush stroke is a pretty loose term that the artist would have to try and guess what you really meant. Which color red? There are hundreds of variations. How “wide” is “wide”? Is it 2 inches, 1 inch, 4 inches? You said 3 inches long, but you didn’t say anything about the curve of the arc. Is it balanced like say half a circle, or is it a curve that’s more pear shaped? Do the end points of the arc start as 2″ wide and stay 2″ wide for the stroke, or do you start with narrow points and build to a 2″ wide stroke at the apex of the arc?
Computers (and that iPhone in your pocket is a computer….) don’t guess. They can’t interpret your thoughts. To a computer, there is a huge difference between “Draw a Red 3″ long arc from the lower left upward” and “Draw an arc, starting at 3″ in from the left, 3″ up from the bottom to 6″ in from the left and 6″ up from the bottom, with an even curve, 2” wide from start to finish using a red that is slightly magenta.
To be a programmer you have to invision the instructions that will eventually lead to what is scene. It’s kind of a double-abstract way of thinking. You have to visualize the instructions for the single brush stroke in addition to how each of the single brush strokes will form the final piece. It’s not for everyone.
What is a program?
In it’s simplest form a program is an Algorithm or a set of rules that precisely defines a sequence of operations. So basically a program is a set of instructions to the computer in a sequence to get to a desired result.
Lets look at a common human task… changing a car tire. How would you tell someone how to change a tire?
- Get the Jack and tire iron out of the trunk
- Remove the lug-nuts
- Jack up the car
- Remove the tire
- Put the new tire on
- Put the lug-nuts back on
- Lower the jack
- Tighten the lug nuts
- Stow the jack and tire iron
That sounds like decent instructions. It’s step by step in an ordered sequence to perform a definite task.
However if you give that to a computer, you will likely have a mess on your hands and the computer will complain madly at you.
Where is the new tire? Where do I get it from? How do I get the jack out of the trunk? How do I jack up the car? How do I remove the lug nuts? What do I do with them? Is there a particular order I need to do them in? What do I do with the old tire?
In this example we could think of each step as a function. Each function has its own set of well ordered steps and instructions do to that finite, desecrate process.
Early in computer programming history you had to write the very finest of details. It would be hundreds of thousands of lines of instructions to cover the simple task of changing a tire.
Over time, we’ve learned to write reusable pieces of instructions and store them in a library so we don’t have to keep rewriting them. Think of “remove a single lug nut” as one of these functions that I can use 5 times to remove all 5 lug nuts. If the function is generic enough, I can give it information about which lug nut to remove and it’s size so we can use the right wrench.
Many of these libraries exist today depending on what you’re trying to do. Many computer languages have a library that provides a function that will draw an arc for you as long as you provide it a length, starting point and a radius and you don’t have the write the actually code to draw the arc.
For the remainder of this series, we will be using a programming language known as Lua which is used in games like World of Warcraft to allow people to taylor the game to their needs. It’s also used by a product known as Corona SDK from Ansca Mobile, which allows programmers to build mobile apps and games for both Apple iOS and Android with one set of code. Lua is also a fairly simple language to learn and it will make a good starting point for us.
Like the tire changing example above. We start with a series of instructions. These instructions are processed one at a time until they are done, in the specific order that they are listed. Thus, the first thing is to get the jack out of the trunk. Next you remove the lug nuts and so on.
If you get these out of order then the program won’t behave. For instance if you jack the car up before you at least loosen the lug nuts you risk having the car fall off the jack. The tire may spin on you making it very hard to remove the lug nuts. Or more clearly, you cannot put the new tire on before you take the old tire off.
Let’s look at something more “computer programming”.
a = 1
b = 5
sum = a + b
In this very simple program we define two variables, “a” and “b” and assign them values, 1 and 5 respectfully. Next we add the two numbers together and store the results in a 3rd variable we have named “sum”. Finally we use a function called print to show us the sum on our screen.
Variables, functions? Whaaa?
Remember those high school algebra classes you said you would never use? Well you are now using them.
Variables can be thought of as containers, like a bunch of Tupperware containers you may have in your cupboard. In programming languages, these containers can hold just one thing (okay, that’s a lie and we will get to that at a later point, but for now, think of it as one container can hold one thing….). That thing can be one of the following:
- A Number… either an integer (whole number) or a floating point number (has decimal fractions, like 3.1425962)
- A String… A collection of letters, numbers and symbols strung together, like this sentence. It could be a single letter, a single word, or a whole document. It could also be a text representation of a number. Strings are typically enclosed in quote marks like: “Barney came home to find Wilma at home”.
- A Boolean… Okay, big word, but it just means the variable holds either True of False. Since computers really only know two things: 1 or 0, a Boolean is the smallest piece of data that represents either true or false. In most programming languages, ZERO (0) represents false, anything else represents true. In others, TRUE and FALSE are distinct names that you use.
Variables can also contain more complex types of data, such as functions, arrays and objects that we will cover in a future post.
In some languages, you have to specifically define what data type goes into a variable, in other languages, any variable can hold any type. Most modern languages, like Lua fall into the later category, were we don’t have to specify specifically what data type goes into its container.
Functions are pre-built sets of instructions that you can provide information to called “Parameters” that help the function work specifically for your needs. Functions are frequently written in a generic fashion so that many programmers can use it with their data and in different ways. You pass these parameters to the function, the function uses the data passed in these parameters to operate on.
In our sample above “print” is a function and we pass it our “sum” variable. That function has a bunch of instructions that knows how to output the variables passed to it to the console or your screen.
Depending on the function, it can possibly take multiple parameters. So we could have done:
print(a, " + ", b, " = ", sum)
to print the whole equation out. This should result in:
1 + 5 = 6
displayed on your console/screen. In this example we passed three variable, each containing a number and two strings, ” + ” and ” = ” to print the various parts.
The computer will start with the first instruction: Assign the number 1 to the variable “a” (put a 1 in the container). It then steps to the next instruction: put a 5 in “b”. Next it will add “a” and “b” together (1 + 5) and store the results in “sum”, finally it prints the results to the screen.
This is a very linear process. Each step, executed in order. When it gets to the last instruction the program is done.
Obviously if you want to write something that does more than add a couple of numbers together, we are going to have to have more control over how the program flows. In the next installment we will cover decision making and flow control.