All three of you who read this may have noticed that I am fond of Java for doing the sort of science-y programming tasks I find necessary to do. I like it because the same code works on both my Mac and my PCs, and because it is reasonably easy to do things using it.
Lately, I have picked up 2 more programming languages, as well as a scripting language. Many popular languages in the programming world are heavily influenced by the
C programming language, a system developed by Bell Labs a long time ago. So to some extent, it is just a matter of learning certain dialects and customs peculiar to a given language to be able to use it.
Microsoft has made some of its
programming tools available free for downloading. The versions that are free are restricted in certain ways, but are still very powerful. I am not aware of exactly what the restrictions are, but for the kind of noodling I am likely to do, the restrictions might as well not exist. These ‘Express’ editions are available for Visual Basic, Visual C++, and Visual C# (the latter two are, not surprisingly, also variants of C).
Better yet, Microsoft has a handful of bloggers who use these editions to do neat stuff, like control disco dance floors and make cool web sites. They bill this as
“Coding4Fun” and aim to involve more experimenter types with programming. Even a rank amateur can find tutorials to get started. I hope lots of people decide to learn to program, if only a little. It makes everything look different, when you can understand a little of the software behind the modern world.
I chose to look at C# (pronounced “See sharp”) for a couple of reasons. First, I am already comfortable with C. Also, C# has been touted as being a lot like Java. I remember from my daze (er, days…) as a physics undergraduate that there was no better way to expose how poorly I understood a problem than to try and work it more than one way. So I hope to solidify my understanding of Java and object-oriented programming with this exercise.
Also, one of my heroes,
Johnny Chung Lee, has done some downright incredible things with C#. So I figured it might be fun to follow his lead a bit.
In my next entry, I’ll regale you with the story of how interesting this has been, and how easy it was to do a few things in C# that are, frankly, a major pain in the ass with Java. But for now, let me just say that I am pleased at how well the system works, and the learning curve has been reasonably gentle, largely because Microsoft has support the creation of lots of interesting examples. Microsoft takes some knocks, but this is a good thing that they do, whatever their motives might be.
The other language I am fiddling with is Objective-C. It, too, is ‘object oriented’. However, unlike the other languages I am using, it departs from the more-or-less standard syntax used for passing messages to objects. To make what this means a bit clearer, suppose I have a clock object, and I want to set it with the current time. In Java, or C#, this would look something like this:
Clock myClock=new Clock();
myClock.setTime(now);
First, I declare a variable myClock to be a Clock object, and by using the ‘new’ keywork and the ‘constructor’ Clock(), I tell the computer to do whatever it needs to in order to get myClock set up to run. By using the ‘dot’ format for message passing, in the next line I tell the clock object named myClock that I want to set the time by having it invoke the setTime( ) function, with the parameter ‘now’. I’m obviously glossing over a lot that some of you don’t know. The details don’t matter- the differences will be clear.
In Objective-C, the same task would look like this:
Clock *myClock=[Clock alloc];
[myClock init];
[myClock setTime: now];
There is a little more tinsel on this one, and I have made it slightly more verbose than necessary, since the first and second lines could be put together, and Objective-C actually has a ‘new’ message that can be used in this context. The asterisk is more interesting, though. In C, this is a way that a variable will be used not for a piece of data, exactly, but rather, to hold the address of a piece of data. The first line says “please allocate a piece of memory big enough for a Clock object (details of which would be specified elsewhere in the program), and return to me the address of this block of memory”. The next line says “using the object ‘pointed to’ by the address in myClock, please do a clock initialization, whatever that is”.
I am not merely being flip when I say, “whatever that is”. A point of using object-oriented programming is to be able to use other bits of software that you might not have developed by knowing only what kind of messages it understands. The details can be ignored, forgotten, or set aside for the moment. All the programmer needs to know is what the object wants in the way of messages, and what it will return in data, or other messages.
Now, this same philosophy is at work in Java and C#. My reason for fooling with Objective-C is that the Mac’s operating system is programmed in Objective-C, and it is the foundation of most of the programming resources available on the Mac.
There are details that I find weird in every language, weird things that are hard in one language and easy in another. Moreover, there are features far deeper than what I have alluded to here that make each particularly strong in certain contexts.
I have left most things unsaid. But I hope, by showing little glimpses of code and descriptions of what is going on, I can give an appreciation of some of the inner workings of computer programs. I will follow up with little examples over the next few posts to make this less abstract.