Monday, January 2, 2012

Code: A Cursory Review of Some Programming Languages

"What's the best programming language?" This is a question I sometimes see at Stack Overflow and other places on the Internet. It's a ridiculous question, of course. There is no one best programming language. Each language has its pros and cons. The question should be "What's the best programming language for what I want to do?" The answer to that question, of course, depends on what you want to do.

In this article, I will review some of the languages I have used and cover their strengths, weaknesses and uses. I must preface this article by admitting that I am still a junior programmer. My opinions may change with more time and further experience.

C++
If I were to recommend one language to learn to any future software engineer, C++ would be it. It's fast, it's powerful, it's compiled, and it's well-known, but most important of all, it's widely used. Many sources put C++ somewhere near the top of the list of the most popular languages, but I personally estimate that it is the most widely used programming language by professional software engineers, with the possible exception of web scripting languages.  It's great for application development and I imagine that most consumer PC software was written in C++ or C. C++ is a superset of C, so it works well with C code and C libraries.

Note: Compilation may or may not be a benefit. To me, compiled languages are preferable because they will work on any computer with the same operating system type. This contrasts with interpreted languages and what I'd call "virtual machine languages" (like Java) that require an interpreter or VM to be installed to use. C++ programs compiled for Windows Vista will work on any Windows 7 computer, regardless of the software installed. Well, there may be issues porting 32-bit programs to 64-bit programs. Also, library dependencies or any other file dependencies could reduce compatibility. Aside from those minor exceptions--which can also affect non-compiled languages--I'd say compiled languages are more portable than non-compiled languages.

There are, however, several drawbacks to using C++. One is that it's not as high level a language as newer languages. Programmers still need to explicitly manage memory. There are several syntax holdovers from C++'s predecessor, C, that I find ineligant.  But the main issue I have with C++ is that it's a huge language that allows you to do things in many ways. To roughly paraphrase a Berkeley computer science professor: "C++ is such a monstrosity of a language that, had it a mother, it would have been killed by its mother at birth." I like to think he was talking about the largeness of the language and how, while it may not be that difficult to learn, it is impossible to master. I don't know if I've met anyone who can claim to know the complete syntax of C++. I am a little embarrassed about how every day I learn something new that I can do in C++.

This may sound like a benefit, that C++ is a flexible, powerful language with many ways to solve a problem. It is not a benefit. It means that reading other people's code can be a nightmare. If the program's author has a unique style, you are constantly trying to see if their coding decisions are even valid C++ code, or why they made the style choices they did.

I hate C++, even though I have a great working knowledge of it. Like a gift of bad whiskey, I tolerate it because I have to and it gets the job done.

C
C is the predecessor to C++. It's also compiled, fast, and pretty powerful. It's often used for drivers and in embedded software. I've been told that there is no reason to write anything in assembly code since an optimized C compiler will do a better job creating a concise, fast program than a human programmer. I like C because it's a small, concise language, yet it can be used to program anything.

The main drawback C is that it's such a low-level language, it has a learning curve (especially when doing complex things with pointers). Similarly, C is a pretty unsafe language that assumes the programmer knows what he or she is doing. A C compiler will never complain when a programmer assigns structs to char pointers, nor when he attempts to access unallocated blocks of memory, making it easy to accidentally cause runtime exceptions.  Almost anything is valid C code.  A computer science professor once opined: "C would compile a phonebook."

Java
Java used to be my favorite language until recently.  It's compiled to an intermediate language, which means you need a Java virtual machine to run Java programs, but Java has reach near-ubiquity on PCs, so most PCs can run Java programs.  The Java VM has had several major updates in the last few years, so the only cause of any compatibility issues is most likely an out-of-date VM.

Java is a nice high-level language.  It's garbage-collected, so memory management is mostly a non-issue and, unlike C and C++, it's quite object-oriented, so it's arguably easier to learn.  The creators of Java obviously learned from the pitfalls of programming in C and C++, and it shows with String handling, the lack of pointers, and its object-oriented-ness.  It's often billed as good language for web applications, but I think it pales in comparison to web scripting languages like Javascript and PHP.  Instead, I'd recommend Java for regular application programming.

GUI programming is relatively painless in Java, and is definitely a pleasant change from MFC.  Java is probably the best language to use if you want to do a cross-platform GUI program, since the GUI libraries are the same for Windows, Linux and OS X.

Java does have a few flaws.  One of the main ones to me is that you can't generate a simple executable that will run on machines without a Java VM--that is, without some special 3rd party compiler.  The other main one is that Java programs are slow when starting up, just-in-time compilation notwithstanding.  These are both issues inherent in virtual machine languages.  C and C++ gurus that love the simplicity and power of pointers may detest their absence.  Also, while Java is pretty powerful, I don't think it's as powerful as C and C++.  Which is why several major programs written in Java (like Eclipse) also have parts written in other languages.

C#
C# is now my favorite language.  It seems to be Microsoft's version of Java.  It's high-level, compiled to a intermediate language (CIL), object-oriented, and garbage-collected.  With out and ref, you get the functionality of pointers.  And while I haven't researched this extensively, C# programs seem to start up faster than Java programs.

The only drawbacks I can see with C# are the facts that it doesn't compile to native code (which may result in some slowness and incompatibility) and that it's Microsoft-proprietary.  Microsoft created the language, but there are no Microsoft C# compilers for non-Windows operating systems.  There is only the Mono project, which I haven't checked out and I doubt has 100% compatibility with C# programs written for MS Visual Studio.

ML and Scheme
I bundled these two together because they are both (usually) interpreted languages that can be used in a read-eval-print loop (REPL) and both are languages I used in college and haven't seen elsewhere.  Also, both can be described as functional languages, especially ML.  As functional languages with REPL interpreters, they are excellent for writing mathematic algorithms and short, quick programs where the programmer can get immediate output.  This contrasts with compiled and imperative-procedural languages where short programs take longer to write and require more steps to compile and run.  But because they are functional, they may be harder to learn for someone who is used to C-based languages.  This could be the reason that they are less widely used.

Ruby
I haven't used this enough to form an informed opinion of it, but I do think Ruby the only language that can claim to be truly object-oriented.  Even the numbers are objects.  Ruby has innovative aspects to its syntax--like blocks and iterators--that make it an intuitive and flexible language that is a pleasure to code in.  However, Ruby is fairly slow, so, like ML and Scheme, I think it's best suited to scripting and small programs.

No comments:

Post a Comment