Tuesday, June 21, 2011

Welcome back, Java

My previous job was almost entirely C++; my new job is almost entirely Java. I think I answered an interview question recently by saying that I like both languages equally, and the one that I choose depends on the needs of the situation.

Of course, I said that having not actually used Java for quite a while. Getting back into Java again reminded me that I do really prefer it to C/C++; that when I first got to use Java, it was liberating, exhilarating; that I couldn't believe I'd dealt with the hassles of C/C++ for so long when Java had been out there, waiting for me.

Say what you want about the relative benefits of both languages; for me, I could make quite a long list of the things that make me prefer Java, but I'll just mention two: one annoying, one more critical:

1. Header Files. Having to maintain header files alongside your source code files in plain C is bad enough. Having to maintain a header file for every class file in C++ is just tedious. And then add how you have to put #ifdef blocks around the content of every header file because C++ can't figure out how to resolve the situation where the same header file is included multiple times. Listen, if I have to add the exact same #ifdef structure to every header file to cope with a reasonably common situation, this ought to be solved by the compiler somehow so I don't have to type the same hacky solution over and over again. Sure, a modern tricked-out IDE will autogenerate all of this code for me, but this is just the IDE glossing over inadequacies of the language.

2. Memory Leaks. As a C/C++ programmer, you cannot ever focus entirely on the desired function of the code, because at least part of your attention always has to be on memory management. Sure, it's possible to write applications that never ever need to explicitly delete or free, but in my experience this is more of a theoretical paradise than a real-world result. I always seem to wind up having to get my hands dirty with pointers and with memory that's my responsibility. It's a profound relief to be able to say "new" in Java without having to immediately make sure that there's a matching "delete" somewhere that will absolutely positively execute.

So goodbye stdio.h, goodbye cout, goodbye char*. You'll always have a special place in my heart.