Wednesday, August 31, 2011

In Defense of Java Over C++

A while ago Tim Robertson elaborated on his opinion that Java compares rather unfavorably to C++, to the point it's not worth learning. His arguments are well known to the seasoned Java professional: limited features, poorer performance, and a sense of missing out on new developments and becoming more of a legacy platform (the infamous "new COBOL" argument).

I beg to disagree. I have worked - and by "work" I mean professional, 8-to-5 programming work - on both C++ and Java projects for years now. They're both very good languages, and when used together can realize great things (hello Android). That said, I do find quite a few advantages to Java over C++:
  • Less cruft. This has become a little less true since 1.5 with "generics", enums and the like, but still Java remains much more concise a language than C++. Arguing that C++ can do everything Java does and then some completely misses this point - often Java is a better tool than C++ precisely because it does less, while also requiring less;
  • A cleaner OO implementation. This is a continuation of my previous point but really deserves its own mention. I learned OO in C++ before getting to know Java, and I was relieved to see how much more straightforward Java's model was;
  • A unified memory model. In C++ you have a dozen ways to manage memory - local variables, malloc / free, the myriad variants of new / delete, etc - while in Java you have a couple primitive numeric types and then everything is dynamically allocated with new and garbage collected;
  • No invalid pointers. In Java an object variable is either null or pointing to a valid object. Whereas in C++ - well, do you feel lucky, punk?
  • No need for header files. It may be just laziness, but I'm really bothered by having to define every functional symbol twice, decide whether to #include dependencies on the header or source file, and so on;
  • Better namespace management. Java's package / import mechanism is a no-brainer, while C++'s namespace feature is more complicated, verbose, and incomplete - besides using symbols you still have to #include headers (did I mention headers suck?);
  • A standard, cross-platform mechanism for dynamic linking built right into the language. Whereas in C++ building a dynamic library is a project in itself (and woe if the thing fails to load), in Java you get it for free, as far as manual labor is concerned;
  • A more complete standard library. Sure, C++ is kind of catching up now - only that "now" actually means "a good ten years later".
What all this adds up to, is that even as C++ is in many respects a more powerful language than Java, programming in Java is a lot easier and quicker; things more often turn out right the first time around; and baffling, mysterious build and/or runtime issues are less frequent. Even when things do go haywire, the tight memory model and pervasive exception mechanism guarantee that you'll at least have a good idea as to where the problem manifested - compare this to C++ error handling, which often leaves programmers staring at a "segmentation fault" error message as though we were supposed to know from that what went wrong.

Because of that, Java is more productive for daily programming, which makes it the preferred tool whenever it is up to the task. Perhaps it's not so much so if you're in academia or NASA, but for contractors implementing custom systems, such ease of development is a pretty big deal: most contracts have far stricter schedule than performance constraints. Getting something without glaring bugs and with acceptable performance now is much more valuable than getting it perfect in two months time.

As for the "new COBOL" argument: first off, COBOL was only ever good to write business rules and the odd input form. It survives today at the expense of financial institutions who've grown so fat and lazy (or ignorant of their own processes) that rather than modernize their operations, they prefer to pay big bucks for a COBOL programmer and tools to keep everything as it ever was. Java on the other hand is all around, from home appliances and handsets to big computer clusters. I myself have used it for pretty much everything, from web service servers to serial port controllers and the odd AI gimmick. So please don't hold your breath for Java's collapse (or better yet, do hold your breath, by all means!) - it's not going to tail off any time soon, and surely no sooner than C++.

Haters gonna hate, but professionals still have to weight on the right tool for the job. Whether it's Java, C++ or something else (I personally I'm very fond of Python), it should be decided on the languages' relative strengths and the task at hand, and not on nerd bigotry.