Why C++?

In a previous post, I had voiced my dislike of C++, and strongly favored coding in Python.  I am of the opinion that you need to choose the language based on the problem you need to solve.  You cannot deny that C++ programs will always be faster and smaller, but that comes with some disadvantages: 

Large C++ programs can quickly unmanageable without the right tools (please use more than just a plain-old text editor).  Sure you could frame a house with a hammer, but why would you, when you have power tools available to you?

The tool I have been using is NetBeans IDE, with a C++ addon**.  I can setup the IDE on my laptop and remote compile the programs to run on my Raspberry Pi.  Click here to read about how to set up the remote SSH connection. 

The weakness for C/C++ is it must be specifically compiled for the machine it is running on.  My laptop is an AMD64 running Windows, I cannot just copy it onto my Raspberry Pi because it is an ARM processor running Linux.  The biggest win for Python is that its interpreter works every architecture (most of the time).  When you don’t care what kind of processor (x86, ARM, 32bit, or 64 bit) you and avoid compiling 5 different versions of your program.  There is a cost for this convenience — the programs will run “slower,” and will use more memory on your system (the interpreter needs more overhead).  But honestly if you, or a user cannot perceive a difference between a “fast” language or a “slow” one, why not pick the easiest option? Memory and processing power is relatively cheap now.

I’m not saying that one language is superior to the other, it’s that they are different tools, and you need to know when to use them.  I will start with Python for the main program, but switch to C if I need certain parts to run faster (when I need a high sample rate, or do video processing).  A beautiful example of how well this works is the numpy library written for Python.  Python’s isn’t great at fast numerical calculation, and iterations (loops).  It’s solution? It calls a function written in Fortran, and calculates 1000x faster.

The strength of the higher level (of abstraction) in Python is that I write closer to English, instead of speaking in code.  You can make not so understandable code in C.  Like what does the following code do?

val = ( *((type *&)(pIP))++ ); //some obscure code

As well less repeating myself… If I create an object Cat in C++,  I really hate typing this to use it:

Cat cat = new Cat();  //here kitty kitty?

The Python way is so much more satisfying:

cat = Cat()

 

**I left out Visual C++ in this post, but it can now create Linux C++ projects, and compile to ARM! (When you install, make sure you check the box to include Linux C++ Workload from the Installer).  It’s worth checking out, but be warned… this is a beast to download (several GB)…click here.

 

Leave a Reply

Your email address will not be published. Required fields are marked *