C++ Generic Coding vs. Java Frameworks
Why are there so many things that C++ developers should do by hand?
Column As an ardent C++ developer, I’m often traumatized by the amount of potentially useful software infrastructure that simply doesn't exist.
When working in Java there are multitudes of frameworks and reusable libraries that allow many problems to be solved at high levels of abstraction without reference to the underlying technology. So why are there so many things the C++ developer must do by hand, which a Java developer can do by reusing existing code?
Is the lack of these frameworks and libraries one of the reasons why the language is losing ground to Java? In this article, I shall examine these points to see if they’re true; and attempt to determine the obstacles to greater code reuse in C++.
Before we start out, let's get a common picture of what type of code we want to reuse. We’ll start by making a distinction between two types of reusable code; a plain old library containing factorised code and the more interesting case containing tools, code-generators and support libraries that provide a means of transforming between the different domains of a data model. An example of the second is an SQL abstraction framework that takes a representation of the SQL schema and generates classes so that the relational data model can be manipulated in the object oriented domain.
In general, the problem of manipulating data in a foreign domain is tedious and time consuming. One approach to interfacing with databases is to call an interface library, perhaps ODBC or even something more proprietary. Such libraries will present everything needed to access the data in its relational form; the code to maintain integrity in the relational model [in purist terms, a proper RDBMS - relational database management system - should maintain its own integrity without relying on the programmer – ed] and the deletes and update code must be written by hand.
This hand-written code is tedious, time-consuming to write and if not carefully managed it will create dependencies on the database interface library everywhere. With an SQL abstraction framework, we use a description of the data model to generate both the SQL and all the code mentioned above.
In other words the mapping from relational to object model is now expressed in generated code, which means, firstly, that it doesn’t need to be written by hand; and, secondly, that the details of handling relational data and interfacing with a database are hidden behind an object oriented interface. Most of us will agree that generating this data model transformation is a significant saving in time compared to writing it by hand. In addition, using the generated object oriented interface means that the code using the database can focus on what it needs to do rather than the details of handling relational data and interfacing with the database.
An example of such a framework in Java is Hibernate. Similar tools for XML and other technologies are plentiful in Java. In C++ this infrastructure is harder to find; it exists but normally needs to be paid for or isn’t as dependable as the Java equivalent. So why is it that we see more of such infrastructure in Java as opposed to C++?
Is it just that Java is the obvious choice of technology for anything involving XML or databases and these frameworks don’t exist in C++ because nobody would use C++ for this type of problem? I’m not sure that this is so self-evident; the languages are not so different that something modelled in one cannot be modelled in the other with more or less the same level of difficulty.
However, just as portability is often a strong argument for using Java for a problem, there is also often a performance argument for using C++; and, for this reason, you could expect that some programmers in any problem space where Java is applied would sometimes prefer C++ solutions.