Apropos Charles’s recent post on C++ and its relationship with Java, here are some notes from my recent, three month stint in C++.
From a technical point of view, the application we were working on was very simple: single user, two-tier design, green-fields development. As a result we side-stepped a lot of the truly interesting C++ problems, such as needing to implement half a garbage collecting memory manager.
errno, through a return code and through exceptions. The three styles don't mix well at all -- you need to pick one and use it consistently.
The Windows API uses return codes, so it makes sense to use return codes when programming C++ on Windows. In our case, we weren’t using Windows, but the client was more comfortable with the return codes than exceptions, so that’s what we used.
Exceptions work well in Java because they are the single, standard way of indicating exceptional conditions, it therefore makes sense to use Exceptions in Java. I can only guess that the reason Joel doesn’t use exceptions in Java is that he is so used to the return-code style.
const is a major PITA. const is one of those ideas that is good in theory, but horrid in practice. The main problem for us was that if one bit of the code is made "const correct":http://www.cis.nctu.edu.tw/chinese/doc/research/c++/C++FAQ-English/const-correctness.html#faq-18.1, all the code around it is "sucked into the const vortex":http://www.cis.nctu.edu.tw/chinese/doc/research/c++/C++FAQ-English/const-correctness.html#faq-18.3.
After a co-worker decided const-correctness was a Good Thing™, I spent several afternoons retrofitting consts all over the codebase. Those afternoons went like this: add a const keyword. Recompile. Find the newly caused const errors. Add another const keyword. Recompile. See one hundred new const errors. Decide a strategic cast of const to non-const might be appropriate. Repeat.1
To be fair, it became less of an issue over the period of a month or so as the consts wormed their way through the codebase. Even so, I don’t think const-ing caught a single error, or helped us write code any faster. Watch Scott Meyer shuffle his feet when Bill Venner puts this to him.
One of the best decisions James Gosling made was to hold off on adding const to Java.
"". "I broke it":http://www.cardboard.nu/archives/000243.html. Seriously! It took four hours to figure out what was going on. Yay for Java immutable strings.This all serves to confirm Java’s credentials for winning over C++ programmers.
1 Actually I used a bit more thought than that, but that’s the general strategy. Next time I won’t whinge. I’ll just do this:
$ find . -name ’.cpp’ -o -name ’.h’ | xargs perl -i -p -e ‘s/const//’
$ cvs commit -m ‘fix compilation errors’
Comments
you think String is immutable?
public String getAString() { //get a reference to the private field value in String class. java.lang.reflect.Field stringValue = String.class.getDeclaredField("value"); //make it accessible stringValue.setAccessible(true); //unsuspecting string String sittingDuck = "sittingDuck!!!!!"; //black magic happening here stringValue.set(sittingDuck, "hastaLaVistaBaby".toCharArray()); //guess the output of this! return "sittingDuck!!!!!"; }Heh. Even so, this is one case where C++ is easier to understand than Java:
char *p = "sittingDuck!!!!";
strcpy(p, "hastaLaVistaBaby");
return "sittingDuck!!!!";
There's another reason to avoid exceptions in C++: You have to target a compiler that implements them poorly, if at all (like the '95 vintage Solaris 2.6 x86 compiler (grumble grumble)).
These restrictions can lead to the absurd state of affairs where the "use of string and collection classes is deprecated" to support legacy compilers.
Think not having GC introduces pointless bugs? Try going back to strcpy() and char* strBuf = malloc(BUFSIZ+1) and see how you like it :(