Thursday, February 14, 2013

Today I Learned: Generic Programming

I read a very interesting article today regarding the differences between Java and C++ from the viewpoint of something called "Generic Programming," and I would like to share the basics of that with you. I am aware that I may lose some of you along the way, however I will do all I can to keep it clear and concise.

Generic programming is a powerful tool that can be used for applications that store a lot of data. For reference, the paper that I read was called  A Comparative Evaluation of Generic Programming in Java and C++ by Hossein Saiedian and Steve Hill. It goes into much further detail onto the specifics and does a better job -I'm sure- of describing the differences, but it is very technical. I am going to attempt explain how this paper proves C++'s implementation of generic programming is more efficient than Java's in a simple and easy to follow way.

To begin, what you need to understand is this: When a computer program interacts with data, it must use either objects or primitive data types. These are specifications that tell the computer what kind of data it is operating on. Primitive data types are rather simple, like an Integer (int) or a Character (char). But objects are data types that can be defined by the programmer, opening up many more opportunities. For example, if I wanted to give my dog a shiny new blue collar I could say something like...

       Dog Chance = new Dog();
       Collar blueCollar = new Collar(Color.BLUE);
       Chance.setCollar(blueCollar);

Here we see the objects 'Dog,' 'Collar', and 'Color' used. Now don't worry if you don't know how to program because that's all you need to understand about that. But if you do know Java programming, and are preparing to complain in the comments that I failed to import Java.awt.Color, all I have to say is I was being "clear and concise!"

These objects, like "Dog," can be part of a larger definition of objects (aka class), like "Pet" or maybe "Animal." In Java all user-defined objects are derived from one class, "Object." In contrast, C++ does not have one giant class to which all other classes belong. Two classes in C++ are not necessarily derived from the same class. This is an important distinction.

Generic programming is a technique of programming that enables the computer to operate on data without actually knowing what type of data it is operating on. The benefits of this include modularity and reusability of code. Let's take an example design of storing data in a numbered list. In C++ this can be done rather simply using vectors, provided to programmers in the standard C++ library, std. A programmer can choose to only allow a certain type of object into a vector list, or one could choose to allow any objects in. Std::vector is a simple way of storing data using generic programming in C++. 

In Java it is also possible to store data in numbered lists, Array or ArrayList being a few common ways, both of which can be initialized without a type parameter, meaning you can create and add to these lists without needing to know the data type. The difference between Java and C++ is when a programmer chooses not to provide a data type to their Array or ArrayList, Java uses the Object type. So while generic programming is written almost exactly the same as in C++, Java's version is not truly generic. 

Why would this matter, you ask? It matters because C++'s version of generic programming is much more efficient. The language was specifically designed with generic programming in mind. Whereas Java's version of generic programming is more of a work-around, and the non-type specific Arrays actually call methods of the type Object which must implement a Container interface, which slows down generic programming for Java.

I found this article very interesting. Deep language analysis like the one seen here is something that I would love to include in my presentation. I hope you followed along for the most part, and I hope you enjoyed this article. Thanks for reading!

-Jeff

4 comments:

  1. So, if I am following here, is this a win for C++ programming? Score "1" for C++?

    ReplyDelete
  2. Yup, you got that right. Though, some may argue that generic programming isn't as used in Java as it is in C++, it is still a score for C++. Thanks for reading!

    ReplyDelete
  3. If Chance is declared as Pet

    Pet Chance = new Dog();
    Collar blueCollar = new Collar(Color.BLUE);
    Chance.setCollar(blueCollar);

    then, in order to pass the compiler check,
    Pet class must have method setCollar(Collar clar) declared.

    ReplyDelete
  4. Exactly, Mr. Chen. Defining classes like "Pet" that encompass more data types is a step in the direction of Generic Programming. Although, I only wrote the blog's pseudo-code to demonstrate the ideas of classes to people who didn't understand them.

    ReplyDelete