Delen via


C++ Mi Amore

I've been programming in C++ for over 8 years and from the first day I was introduced to it, I knew it was love. I find it very hard to explain to people my obsession with this language, but I'll do my best to articulate it while addressing some of the arguments opposing it from its critics.

I view software engineering as a combination of two equally important components: engineering and art.Programming languages are the software engineer's equivalent of the painting brush,colors, and canvas of a painter. It is the form by which software engineers express their art. Every computer science freshman knows that all programming languages are computationally equivalent to each other. So the real difference between one programming language to the other is the extent to which it allows you to express your ideas, and your thoughts. 

C++ is one of the few programming languages that allow you to express your "software art" in the most versatile manner without sacrificing performance or efficiency. With C++, one can write embedded software in which the system imposes resource limitation and in which every byte counts. One can write real time software which requires temporal correctness and in which every cycle counts. But in addition, one can write beautiful and elegant high level abstractions. Abstractions that are usually attributed to managed languages such as Java or C#. 

C++ lends itself nicely to the procedural programming paradigm, to the object oriented programming paradigm and to some extent with C++11 to the functional programming paradigm. But, if you ask my opinion, and you are by the virtue of reading this post, the creme de la creme of the language are C++ templates. A C++ newbie is usually introduced to templates as the means of creating generic data structures. However, the standard template library as well as the BOOST library use templates as the means of creating generic programs - programs in which the algorithms are independent of the types of the objects that they operate on. Templates are by far superior to their "generics" counterparts in Java and C#. Most notably due to their compile time nature, the ability to specify non type template arguments and the ability to customize them using different types of specializations. 

In his masterpiece "Modern C++ Design", C++ guru - Andrei Alexandrescu showed how the main "GOF" design patterns could be implemented in terms of templates. He introduced new concepts such as policy based design and his famous typelistswhich revolutionized the way we view and use templates in C++. To close the templates discussion, the most amazing aspect of C++ templates is that they form a meta language which is interpreted by the compiler, making them Turing complete. Yes, that's right. You can use C++ templates to write programs that get executed at compile time. 

This arguably makes them the most powerful and remarkable feature of them all. 

So now that we talked about how great C++ is and especially how marvelous C++ templates are, it is time to address the question of why it is that C++ is not as widely adopted as other languages such as C#, Java or even its predecessor C ?

I view the C++ opposition as roughly divided into two. The first which consists of managed code developers such as Java/C#/etc... , claim that C++ lacks the richness in libraries that the other high level languages have. They also don't like the fact that C++ still contains "low level" features such as pointers and lack a built in garbage collection, forcing them to handle memory management manually. The second group of opposition comes from the C community. They claim that C++ is not as efficient as C. They claim that the compiler's "magic" that happens behind the back of the programmer such as memory allocations, virtual function and so forth is harmful. Lastly, they argue that C++ is hard to read,maintain or to write to begin with. In essence the hard core C programmers do not see C++ adequate for systems programming or they find it too complicated to be used. So, is C++ too high level ? or is it too low level ? As we previously mentioned, it is actually both. 

To the first group I answer that C++ can be used to create all the libraries that are available in any other high level languages but much more efficiently and optimized better for the underlying system on which it runs. For those missing automatic memory management (or garbage collection) I would recommend the X_ptr family of objects (replace X with shared,unique,weak) which were introduced in the BOOST library and are now part of the new standard. If the lack of built in garbage collection is causing you sleep deprivation, then by all means download one from the web or even better, write one yourself. In addition there is an ongoing effort in the C++ community to add more native general purpose libraries similar to those that exist in the other high level (managed) languages. A great example of such a library is the native C++ Rest API Library (aka Project Casablanca) from Microsoft. 

To the second group (the C programmers) I begin my answer by reminding that C++ is a super set of C. You can write perfectly good C code (with some minor restrictions) using C++. But that's not really a valid point is it ? why would you bother writing C in C++ ? The truth is that I agree with the notion that C++ is a tough language. It's "toughness" is a byproduct of its flexibility, which we previously discussed. Is this a reason to avoid it ? of course not. First, every programmer should have the self awareness if he or she are capable of using a tool or a programming language in production code. Second, the most important thing is to choose the right tools for the right task, and C++ is flexible enough to be the right tool for almost every task. You just need to learn how to use it.

As an example, there is an ongoing trend in the embedded software community to use more of C++ to develop middleware and even to develop device drivers which were once only implemented in C or assembly language. Lastly, there is no compiler "magic" or trickery going on that any seasoned C++ programmer can't foresee or understand. As a matter of fact I'm sure that expert C++ programmers can translate C++ lines of code into their equivalent assembly language instructions just as expert C programmer can do. This is again related to the notion of knowing the tools you are using and how to use them properly.

If you know the language then you know its strengths and you know its weaknesses. The beauty of C++ is that you can pick and choose. You can decide how low level you want to go or how high level you want to go. Nothing is stopping you from expressing your inner software artist. To beginner C++ programmers I say, don't despair. C++ has a very steep learning curve, but its worth its tuition. Once you master it, you'll feel that you can achieve anything computationally possible in any way you want with very few limitations. Its a trip worth taking, and it never ends. I still find myself learning new things about the language every day, which is worthwhile by itself.

Comments

  • Anonymous
    January 01, 2003
    thank you