iPhone Programming 101, part Three: Languages
Another in a very occasional series of columns covering the craft of programming and what it takes to create one of those app things.
Even though this article is about computer languages it is not meant at all to teach one how to “speak” in any of these languages, for that is light-years beyond the scope of this article.
A computer program is likened to a recipe. A recipe with potentially millions of steps that could come crashing down in a smoldering heap of code if as much as one of those steps is in the wrong order. The recipe might tell the system that if the user does something, then load in an image, draw it to the screen in green, rotate three times, do the hokey-pokey and fade it out. And that might be just one little task of thousands in a complex web of tasks, actions and behaviors.
As with any kitchen recipe, there is a specific lingo invoked that serves as a precise form of shorthand that the iPhone can understand as well as the programmer. And it is this shorthand that forms the basis of a computer “language.”
The earliest computers were programmed at the lowest level, in bits and bytes; frequently hand-entered by switches on the front of the machine, paper tape or even punch cards. It was a system that was extremely tedious, highly error prone and very hard to read. Back to the kitchen analogy: think of the instruction to take a cup of flour and mix it with one egg. Short and to the point. But the earliest machines didn’t know what flour, a cup or an egg might be. So the recipe would now have to actually instruct how to make a measuring cup, how to grow and harvest wheat, then grind it up into flour, and…well, you get the point. There had to be a better way, and as a result, FORTRAN (FORMula TRANslator) was invented in 1954 at IBM. Considered the first modern computer language, it used a combination of basic mathematical symbols, punctuation and simple English words to describe program flow. Instead of having to describe how to raise wheat, the system now already would understand what “wheat” really was. Very quickly other languages were developed such as COBOL, LISP and ALGOL. And many other that are still in use today.
Why so many languages? Languages are generally tailored to different tasks. One might be science-oriented such as FORTRAN, and another database-oriented such as SQL. Apple’s OS-X and iPhone OS are programmed in what had generally been a somewhat obscure language called “Objective-C.”
In the 1970s, a new language came into use called “C.” It was immensely popular in that it was flexible on a high level, while still having features that made it fast and efficient on a lower level. Over time, variants of C came out using a new programming model that emphasized reuse as a necessary component in software as programs became more complex. Known as “object oriented languages” these structured the recipes in smaller and more easily understood and reusable steps. The measuring cup could now be reused for different recipes. Out of this came “C++” (pronounced “C-Plus-Plus”) that is one of the most popular languages today.
At about the same time a much simpler C-variant called “Objective-C” was released and picked up by the NeXT Computer Corporation for use in developing their tools and applications for their curious but advanced cube-shaped computers. NeXT was founded by Steven Jobs, and when bought by Apple in 1996, Objective-C became the language of choice used to access the inner workings of what would be the new OS-X operating system.
So what exactly does a computer program look like? In the case of Distant Suns, it looks like about 80,000 lines of “code,” some of which you see below:
m_TGLabel.text=@”Let’s tour tonight’s best objects!”;
if([m_Prefs isOn:DS_PREFS_TOURGUIDE showCore:false])
if([m_Prefs isOn:DS_PREFS_NIGHTVISION showCore:false])
This creates the Tour Guide toolbar that is used to help someone find the highlights in the sky during a particular season. “m_TourToolbar” is considered an object, something that has properties such as size, location and transparency. “m_TGlabel” is a text-object that is given to the toolbar to display a summary of each planet or star. Lather, rinse and repeat things like this for 80,000 lines and you have a very cool program.
Once a piece of the program is written, it is compiled into the very same low-level machine code that engineers used to have to program in by hand. Here is what part of the machine language version looks like:
addr1, sp, #364
addr3, r1, r3
addr3, r2, r3
Apple’s development environment provides a wide set of prewritten tools, the measuring cups from the kitchen example. These are usually referred to as libraries or frameworks. So for example, Distant Suns makes use of the MediaPlayer framework to handle some of the audio duties. The OpenGLES framework is used to render all of the 3D graphics (in both Distant Suns and for the normal iPhone screens as well). The UIKit.framework contains all of the prebuilt tools to construct the standard iPhone user interface elements such as toolbars, buttons, scrolling lists and so on. It is the elegance and sophistication of these libraries that really make programming Apple’s devices a pleasure, compared to Microsoft Windows, Blackberry or Nokia’s Symbian based machines.
Yes, doing a world-class commercial piece of software can be daunting. It was much more so in the early days when programmers had to “harvest the wheat” themselves. But if you ever wanted to give programming a shot, even the most simple exercises can give one a great sense of achievement, not to mention help a person learn how to think more logically on problems of the real world.