Saturday, August 15, 2009

The Beginning

Programming doesn't have to be difficult... it may often be challenging, but life without challenge sounds terribly boring. Lets start with the abstract.

What is programming?

Programming is, in the most basic sense, the process of writing language that a computer can use as instructions. Despite the apparent simplicity, this is what makes programming so difficult! Computers can only do what you tell them, exactly what you tell them, and telling them what you want can be challenging... you have to know what you want to begin with, only then can you begin to worry about how to tell them what it is you want. Interestingly, the process of laying out what you want is guided (if not informed) by the language in which you tell the computer. For instance, suppose you want the computer to pick a bunch of numbers. To a computer, this is meaningless! The computer needs to know what kind of numbers you want, how big they are, where they're going to be stored and so on. By having to be so very specific, you are automatically required to know the details so that you can specify them. This seems quite tedious, but the upshot is when you have your instructions perfect, the computer will give you exactly what you want every single time, and inconceivably fast.

A computer program can be considered an imaginary machine that works with numbers, concepts, and ideas instead of gears, levers, pulleys, etc. When we write a computer program, we aim to build an imaginary machine in much the same way a business that makes blenders - in order for a customer to use the blender, they don't need to know anything about how blenders work. When I push "blend" on my blender, it blends. If I had to pull apart the blender and flip a switch somewhere inside of it every time I wanted to make a smoothie, well that would have to be one of the worst blenders ever designed. Thus, suppose we had an imaginary machine called getWholeNumbers() - when we press "go" on getWholeNumbers(), we expect to get some whole numbers regardless of what happens inside the imaginary machine. The importance of this will become clear later, for now all I can do is promise that it is important, even when you are both designer and user of these imaginary machines. This design philosophy goes under the name object-oriented programming (OOP), something to be covered in greater depth later.

The getWholeNumbers() machine is officially referred to as a method. A method is in most ways just like it sounds, a way of doing something. A method can also be thought of as the "blend" button on a blender, but since we're working inside computers, instead of getting a delicious smoothie we get back the concept of a smoothie. In java, pressing "go" on methods is referred to as a call - a line of code calls a method or does a method call. Maybe it will help you to remember if you imagine a blender that can only blend when you call it from your phone, and that a blender is a method of blending.

A method call looks something like this:

methodName();

Suppose we have a method that gets a whole number in the range from 0 to 9. A whole number is called an integer, so it might look like this:

getInt0to9();

But what exactly does this method do? It isn't clear from the name alone, maybe it gets all integers (ints or int now on) from 0 to 9, or maybe it gets two ints, 0 and 9. When we call a method it is essential that we know what it is doing. As such, every method has what is called a return type, which is exactly what it sounds like, the type of thing it returns. As our programs become more complex we will need more information about what a method does so we can use it without it breaking our program. The source of this information is generally called documentation, more specifically the Application Programming Interface (API), which is a needlessly complicated and uninformative name; you can just think of the API as a kind of encyclopedia for programming, it contains all the information a person might need to use a pre-written method. The java API can be found here, though you might as well wait before you go there for the first time... no matter when you go the first time is intimidating (if only for the magnitude of information there), but the longer you wait the more you will understand and less fear it will bring.

Lets not get ahead of ourselves though, programming, just like everything else, is best learned by jumping in head first - over time, the pieces fall together.

No comments:

Post a Comment