Wednesday, June 06, 2007

Java Versus C

I just finished, more or less, a Palm OS application in C. Strictly speaking, it is not C, rather, it is using C++ to write a C program. In other words, I am not using any object-oriented features of C++.

Programming in that style of C is actually the only way to control exactly what is going on in terms of memory for Palm OS. You see, Palm OS has no memory protection; an error that causes a segmentation fault on Unix will cause a Palm OS device to reboot. Because of that, I became careful about where every byte is. With C++, it is tougher to account of the bytes, as the virtue of encapsulation also means opacity some times.

After the application is done, I started thinking about the experience I had, and the experience I had for the last few years, when I programmed in Java almost exclusively, other than some Javascript, and Perl. All of these languages handle memory for the programmer.

In terms of programming, all of the languages are fine. They have different styles. But I am not a language bigot; I use a language when it is the right language to use, or it is the best language to use. In a particular context.

I can now say that an incredibly big advantage of using Java (or Javascript, or Perl...) is that refactoring is easy for these languages and very practical especially with an IDE like Eclipse.

For C, especially for an operating system like Palm OS, refactoring is a hairy thing. Instead of trying to find code to refactor, I found myself making sure every change is a necessary change. After a few reboots that took forever to figure out, I became a lot more defensive in my programming, and I also became very defensive in terms of changing anything.

I believe that is the true cost of not having automatic memory management in a language: it make programmer not to have the courage to perform major re-factoring.

Maybe Palm OS is an exception, as it does not have very good debugging facilities, and the simulator is one version down, the emulator two versions down, and device debugging is not fun, to say the least. But still, the disadvantage is real, if quality is concerned. Refactoring is the only way to increase quality of an application, especially if it changes frequently.

No comments: