C++ Isn’t Cool I August 14, 2006Posted by gordonwatts in computers.
Orin, a UW student working with us:
everyone around here is so down on c++… but even though it gets you into trouble once in a while, I really like having to deal with these issues; you get to see what is really going on, and it’s kind of a beautiful monstrosity
This was after several hours of work trying to track down a problem deep in some code that I’d originally written and he was trying to modify. Basically, the error came down to replacing the line
TMBJet jet = GoodJet[i];
TMBJet &jet = GoodJet[i];
This is one of my complaints about C++ — that one character change dramatically changes the meaning of those two lines. The first line takes an object of TMBJet, stored in the GoodJet array, makes a copy of it, and stores it in the new object called jet. The second line mearly makes a reference to it – so jet becomes shorthand for referring to the I’th entry of the GoodJet array. That is a lot of meaning for a simple ampersand character!
C++ is filled with sort of thing. If you miss one of them you can spend hours debugging. I’m pretty good at C++ and I still make this sort of mistake.
In some ways C++ reminds me of perl and teco. The first, a scripting language, and the second, an old old editor, have in common a very compact and complex syntax. Single letters and punctuation can dramatically change the meaning of the command. This is great for the programmer: you can write very compact and complex instructions with very few keystrokes. But trying to read what you did 6 months later… And old joke went “Can you predict how a text file will change if you type your name into teco?”
Orin is right, however, the monstrosity below – and its complexity – can be pretty cool. We had some great people write the framework that we use, and learning its little tips and tricks does brings with it a certain satisfaction. Sadly, this often gets in the way of getting physics done!
UPDATE: This was meant to come out before the “II” version of this, but I accidentally kept this as a draft. Sorry about that!