iPhone Programming 101, part Three: Languages

Posted by on May 23, 2010 in Essay, iPhone/iPad, Programming | 4 comments

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=[UILabel alloc];

m_TGLabel=[initWithFrame:CGRectMake(10,0,rect.size.width,rect.size.height)];

m_TGLabel.backgroundColor=[UIColorclearColor];

m_TGLabel.font=[UIFontboldSystemFontOfSize:12];

m_TGLabel.lineBreakMode=UILineBreakModeWordWrap;

m_TGLabel.numberOfLines=2;

m_TGLabel.textColor=[UIColorgreenColor];

m_TGLabel.text=@”Let’s tour tonight’s best objects!”;

[m_TourToolbar setFrame:CGRectMake(0,mainViewBounds.size.height-(toolbarHeight+tour_guide_toolbarHeight),mainViewBounds.size.width,tour_guide_toolbarHeight)];

m_TourToolbar.alpha=DS_PREFS_TOOLBAR_ALPHA;

m_TourToolbar.hidden=YES;

[m_TourToolbaraddSubview:m_TGLabel];

if([m_Prefs isOn:DS_PREFS_TOURGUIDE showCore:false])

m_TourToolbar.hidden=FALSE;

else

m_TourToolbar.hidden=TRUE;

if([m_Prefs isOn:DS_PREFS_NIGHTVISION showCore:false])

{

m_TourToolbar.alpha=DS_PREFS_TOOLBAR_NV_ALPHA;

}


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:

L3390:

.long304

.long0

.long324

.long_OBJC_IVAR_$_distantsunsAppDelegate.m_TourToolbar-4-(L3213)

.longL_ZL28_OBJC_SELECTOR_REFERENCES_97-4-(L3214)

.long1061158912

.long_OBJC_IVAR_$_distantsunsAppDelegate.m_TourToolbar-4-(L3215)

.longL_ZL28_OBJC_SELECTOR_REFERENCES_95-4-(L3216)

.long_OBJC_IVAR_$_distantsunsAppDelegate.m_TourToolbar-4-(L3217)

.longL_ZL28_OBJC_SELECTOR_REFERENCES_52-4-(L3218)

.long_OBJC_IVAR_$_distantsunsAppDelegate.m_TGLabel-4-(L3219)

.long_OBJC_IVAR_$_distantsunsAppDelegate.m_Prefs-4-(L3220)

.longL_ZL28_OBJC_SELECTOR_REFERENCES_96-4-(L3221)

.long_OBJC_IVAR_$_distantsunsAppDelegate.m_TourToolbar-4-(L3224)

.longL_ZL28_OBJC_SELECTOR_REFERENCES_95-4-(L3225)

L3222:

.loc311660

ldrr3, L3392

addr1, sp, #364

addr3, r1, r3

ldrr2, [r3]

ldrr3, L3392+4

L3227:

addr3, pc

ldrr3, [r3]

addr3, r2, r3

ldrr3, [r3]

movr2, 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.