Episodic Genius


occurring occasionally and at irregular intervals


Reconciling Differences

I'm pretty handy with C++. I know many of its deepest, darkest secrets and was once proud to understand things like iterator classes, multiple inheritance and template specialization.

About three years ago, as part of a semi-desperate lateral move inside my company, I found myself suddenly in the Java world. I picked it up with little effort and before our project was off the ground I was the local expert on many advanced Java topics. I had some experience with pre-Java 5 years before. I found that Java Generics are rather elegant and are a huge readability improvement but I really had to understand the difference between them and C++ templates to make them useful. Java's standard annotations are quite useful and good but I always had a feeling that there are many more bad ways to use annotations than good ones.

Possibly the best thing new to Java 5 is the enumerated types. I've never met someone other than myself who actually implemented a C++ version of the typesafe enum pattern from Josh Bloch's first edition of effective Java. As far as I know, I may be the only one! My experience with this pattern and the problems it solved in the very large software project I implemented it for left me with a profound appreciation for the implementation in the Java 5.

Around the middle of this year, I started working on a rather ambitious personal project. Progress is extremely slow because I've got a family and I still have my day job but I'm getting more disciplined and as I do, I crank good code out a little faster.

My first roadblock was something of an identity crisis. I hadn't lost my C++ chops but I now had a this brand new set of Java chops. I wasn't sure how much I the two could mix. Some things that I used to love (or at least didn't hate) about C++ really bothered me now. My old C++ style didn't seem right anymore but I couldn't simply adopt Java style in C++ because certain things just don't translate. I'll do a full post dedicated to my split coding personality at a later date. For now, we'll just say that it took me a while to work out a style that I am comfortable with.

My project, as it stands, is a cloud based application with a Java and a C++ component. I got addicted to test driven development with Junit and EasyMock so I looked in to some similar frameworks in C++. I settled on the Google Test and Mock frameworks.

Going back to C++ I found I miss the memory management in Java. For interaction between major components in the system, I code to interfaces and use boost's shared_ptr for memory management. I found a few other components from the boost libraries useful.

Another trick I learned from Java is how to use multiple inheritance. Java allows it, but only for multiple interface base classes. I'm using a similar pattern in C++ where I allow multiple inheritance if the multiple base classes meet certain criteria similar to Java interface classes. Mostly, all virtual methods must be pure and the class cannot have any data. I make sure to use virtual inheritance for such base classes to avoid ambiguity when the inheritance hierarchy gets a few levels.

Mastering a few very different programming languages has its advantages. I've also attained a certain level of expertise with ruby, bash and perl. I think that one's ability to program in any one of them is enhanced by knowing the others. It isn't to say that there is one ultimate style that should be used across them all but rather each encourages some good programming patterns. Some of these patterns may be too difficult to implement in other languages but there are some that can bring significant benefit.