Thursday, July 14, 2016

Programming Languages

As a computer programmer the language I prefer to program in is obviously an important subject.  In my opinion C/C++ is the best all-around programming language.  When I was learning to program in the mid '90s, C/C++ was the undeniable king of programming languages.  However, in the time since then I feel like the industry has moved away from C/C++ to the point where some developers consider me to be a "dinosaur" for using such an old language.  But I don't think I'm a dinosaur, so I wanted to talk about languages in general and defend my preferences.

There are literally hundreds if not thousands of programming languages.  If you're learning programming for the first time or if you're starting a new project, how do you know which language to select?  Programming languages breakdown into 4 categories; 1 low-level category and 3 high-level categories.

Assembly:
Assembly is the only low-level language where the developer writes code targeted at a specific hardware platform.  Whereas hand-written assembly can be used for general purpose programming, it's very difficult to manage and nowadays assembly is mostly reserved for very specific cases, typically firmware and embedded programming systems.

Native Machine Languages:
Native machine languages are high-level languages that are compiled to target a specific platform.  That is to say, if your code will run on different platforms you must compile your code once for each platform it will run on.  This category includes languages like C/C++, Pascal, Fortran, Cobol, and Ada.

Virtual Machine Languages:
Virtual machine languages are similar to native in that they are compiled.  But the output is not hardware or platform specific.  Instead the output is "virtual" machine code that must be converted at runtime to the native machine code for the platform where your code is running.  The benefit is the developer compiles once and releases a single binary, but it can run on multiple platforms.  This category includes Java and C#.

Interpreted Languages:
Interpreted languages (often times called scripted languages) are not compiled.  Instead at runtime a program called the interpreter converts the code from human-readable to machine code.  Interpreted languages have the benefit of not needing the slow compilation process and can be quickly changed or tweaked.  This category includes Python, Perl, PHP, Javascript, Ruby, and many more.


So how do you pick a language?  What is the best programming language?  Well, the answer is the right tool for the right job.  All languages have pros and cons, so picking the best language requires an understanding of those.

Skipping assembly, because it's not really general purpose in today's world, let's talk about interpreted languages first.  I might take a lot of flak for this, but I don't consider interpreted languages to have much use outside of academia.  Interpreted languages are perfect for learning the basics of programming.  Because these languages don't require the developer to think about things like variable types, memory management, strings, etc. they allow the person to focus on learning the basics of programming.  After all, pilots don't fly a 747 as their first plane, they start on small propeller planes.  To me the only use for interpreted languages is when used as a scripting language to speed up tasks and eliminate human errors.  For example, say an IT person needs to create a new user account, add a machine to the domain, and assign that user to the machine.  This is a perfect task for a scripting language.

But languages like Javascript, Python, PHP, and Ruby are some of the most popular programming languages, so what's wrong with these languages?  The problem is speed and efficiency, or lack thereof.  Because these languages are interpreted at runtime there are significantly slower than other languages.  A recent comparison found these languages can be as much as 400x slower than a natively compiled program.  Probably a good rule of thumb is expect between 10x and 100x slower performance compared to natively programs.  That is a huge price to pay.  I'm dumbfounded that so many server applications are running in languages like Javascript.  That's like entering a car race with a bus.


Ok, so that leaves us natively and virtually compiled programs.  Here too native programs have the performance advantage, although it's much less pronounced.  A recent study found that on average C/C++ outperforms Java/C# by 20%.  On the flip side, Java/C# can be faster and easier to develop in compared to C/C++.  For many software companies, their highest cost is developers' salaries.  So picking Java or C# may result in quicker development times which results in lower development costs.  This all assumes your application is not performance critical (which most client/desktop applications aren't).

However, there is another side to the equation which developers rarely consider.  If your application runs on the server side, that extra 20% performance increase by going with C/C++ translates directly into 20% less electricity used by the servers.  It also means less electricity for cooling the server room which saves even more money.  When you start talking about applications that run on servers, suddenly the hardware for those servers, electricity used to run those servers, and the cooling costs for the servers outweighs development costs.  So in cases like this a native language is probably your best choice.

There is another benefit to native languages - extended battery life.  In that same study above, they found that C/C++ programs ran for approx. 20% longer on a given battery charge compared Java/C#.  So anyone developing applications that will run on a laptop, tablet, or phone should seriously consider a native language.

It is true that C/C++ is not the king it once was.  However, when you group C/C++ together it's still the number 1 programming language ahead of Java.  Also, any programming field where you need performance (e.g. video games) then C/C++ is the language of choice.  Yet another example is operating systems.  Every major operating system in existence is written in C - which includes Windows, Linux, Mac, Unix.  Even "mobile" operating systems like Android are built on top of Linux which is written in C.

So without a doubt C/C++ has a place in the modern world.  I think it's unfortunate that so many developers have gotten caught up in the virtual and interpreted language hype and forgotten about the power of a native programming language.  That said, recently there has been a resurgence in native programming languages driven largely by mobile devices with their relatively low resources and battery power, they are the perfect platform for a native programming language.

No comments:

Post a Comment