Tuesday, August 25, 2009

Start Programming Quick and Dirty

Warning: This really is a quick and dirty start to computer programming, and if you know anything about software development, I'm sure you will find plenty of defects in my crude descriptions of computing. The point here is to communicate the basic understanding to begin learning without submarining a person's brain with useless detail. If a beginning programmer learns the quick and dirty way, then as their knowledge and interest grows, they can go back and fill in all the little details later. When you are writing computer code, or even just using a computer, it really helps if you keep in mind the three things that make up all computing devices.
  1. The processor.
  2. The memory.
  3. The storage.
These are the building blocks of every computer program and the only things we know for sure that all computing devices have in common. An iPhone, a laptop, or a server; Each of these computers has these three components in common. 1. Processor The processor is a collection of transistors that can do simple computing operations like evaluating if two bytes are equal, or adding two integers together, etc. A processor fetches values from memory, performs operations on them, and then puts them back into memory. 2. Memory System memory is a collection of computer chips that are able to store bits of information while the power is on. As soon as these memory chips lose power, the information is lost. Inside each memory chip there are 'rows' of data, and each 'row' is addressed with a special addressing number. The processor can remember where values are stored in memory using these addresses, but as soon as the power is shut off, it is all gone. 3. Storage The system storage can come in two forms. First is a set of spinning disks called a 'disk drive' and the second is a set of computer chips called 'solid state'. Most desktop and laptop computers have disk drives, while cell phones and memory sticks use solid state. Disk drives can be thought of like a record player, but imagine a record player that could play records and write them too. Solid state storage devices are banks of computer chips that do not lose the information stored in them when they lose power. So how does a programmer link these 3 fundamental computing functions together to make a computer program? We can demonstrate this process with a program called Python. Python is actually a computer programming language, which we will learn more about later. But for now, you should start the Python interpreter in your terminal by simply typing: python and hitting enter. (if you do not know what a 'terminal' is, please post a comment and I'll send you some more detailed instructions. If you type 'python' in you terminal and nothing happens, then you probably need to install it. Some good instructions can be found here: http://diveintopython.org/installing_python/index.html ) You should see something like this on your screen: Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> The '>>>' is what we will call the Python prompt. Now enter in this program, hitting enter after you have typed in each line: >>> myvar = 1 >>> myOtherVar = 2 >>> mySum = myvar + myOtherVar >>> print myvar >>> print myOtherVar >>> print mySum See what has happened? First we stored a value, the integer 1, in the computer's memory. Remember that each value stored in memory has an address? Well that address would look something like 'f8a9b6' to the computer, which is really hard to remember for humans when we are trying to write a computer program. So, Python does us a favor and remembers that when we refer to 'myvar' we are really referring to the 1 we stored at address 'f8a9b6'. Second we stored another value, this time the integer '2' into memory. Now, if we were to keep adding values to memory without removing any of them we would eventually fill it up and cause our computer to crash. So, the Python interpreter also does something else really useful for us; It can tell when we are done with a value and it will remove unused values from memory so that we can go on with our program without having to remember to remove all our values when we're done with them. Third, we told Python to add the two values together. However, adding two values together is not much fun unless we store the result somewhere in memory so we can use it again. What actually happened here is that the Python interpreter remembered the address in memory pointed to by 'myvar' and 'myOtherVar', and then sent those addresses to the processor along with the instruction to add them together. A processor always does exactly as it is told, and so it fetched the values from their addresses in memory, added them together and put the result back into the memory location that Python pointed to with 'mySum'. Last, we used the 'print' statement of Python to retrieve each of our values from memory and print them out on the screen. Statements like 'print' are just shortcuts for longer instructions to the processor. So instead of saying: >>> send value from memory address at myVar to the graphics card to display We can just say: >>> print myVar or instead of saying: >>> take the value at myInteger and add 1 we can say >>> myInteger++ This is why computer programmers often refer to the terms 'writing code'. Because, all a programming language really is, is a set of statements or verbs that tell the computer to do something. So, instead of writing out long instructions in plain English, each programming language has a list of codes we can use to save time and space. So, that is only half of the story on a programming language. The other half is what is called the interpreter or compiler. We don't need to get into the specifics on each of these but what we do need to know is that the programming language we are writing must be translated out of the code, and into a language the computer can understand. Computers don't understand English, Latin, or even any programming languages for that matter. The only thing that the processor, memory, and storage understand is something called machine-code. Machine-code is written as a series of 1's and 0's like this: 100011 00011 01000 00000 00001 000100 While this is the only thing that a computer understands, it is very hard for humans to read and write, so in the early days of computing some smart folks came up with an answer. They would create an interpreter to translate machine-code into a programming language and vice verse. Today there are many different kinds of interpreters and compilers, one for each of the many different programming languages. Incidentally, an interpreter or compiler is a computer program written in machine code, but the nice thing about them is that once an interpreter or compiler program is completed by the smart people who write them, the rest of us can use it to write our programs in a language a little easier to understand. So, with a basic understanding of what is going on, and the right programming language interpreters in hand, we can tackle larger problems, like designing the interface to our program. There are many ways to do this, but one of the most popular, and the easiest to learn is XML and CSS. We'll move on to those later. At this point, I would encourage you to continue to learn a little more Python, and this is one of my favorite tutorials: Dive Into Python You can skip section 1, but you should thoroughly understand sections 2 and 3.