Compiled Versus Interpreted Languages?

Many people seem to have this idea that interpreted languages are somehow not a proper programming language (as opposed to compiled languages). Well if you ask me that is false. While I agree that some languages are better at some things than others, the fact that language is compiled or not should not be the main factor by which you should be judging its suitability for some given purpose.

In order to understand what i mean with this claim you need to understand this: in order for a program written in any language to run it needs to be translated to binary CPU instructions. This same rule applies to all languages (starting with languages considered to be serious and ending with esoteric languages like Brainf*ck).


Compiled Versus Interpreted Languages?

So this is where things get interesting. There is few ways to achieve the end result (binary CPU instructions). The two most popular ways is to either compile it to binary instructions once(C, C++..)or use an interpreter that goes through all compilation stages every time program is ran (PHP, Ruby, Python, Javascript…).

So all is good, can I finish this article now? Well.. not really. Because if you ask me the above examples is merely a default option. Would it be concievable to write an interprete for C? Well yeah.. there are many outhere already, here is just one: CH – interpreter for C/C++. Same goes for the interpreted (by default) languages like PHP, as well there is many compilers available for it. Here is the one made by Facebook.

So now that we have established that wether language is compiled or interpreted is merely a default recomendation by people that developed the language, lets take a look why did they choose one design over other.

Lets starts with C, which is one of the most popular compiled languages. So why? Well first of all it was designed with a specific purpose: to rewrite the Unix operating system which was written in Assembly language (which is totally architecture dependent) in a language which would be more portable. So in order to be able to run C code it needed to be compiled to binary instructions, because it was meant to be run on “bare metal”.

Another important reason is that C is statically typed language. Which means that every single variable have to be declared with a specific data type associated with it (integer, char, float etc…). And type checking (checking that for example you are not trying to assign character to a variable with type integer) is the most expensive step. However because you only need to do it once (in compile time), as opposed to everytime (when C is run in interpeter) it can be tolerated. But if production code of large application written in C was run with interpreter (which obviously is possible) there would be huge performance issues.

Now lets looks at why for example PHP is not compiled by default. Well first of all it is dynamically typed. So with the most expensive operation of compilation process out of the question going for interpreter offers some nice advantages. Most obvious being one step less to actually running your program, which is always a good thing. Another nice thing is that it becomes easier to share programs with other people. Because it can be ran “as is” without any extra steps on any platform (as long as there is PHP interpreter written for that platform).

So while there is reasons why language is by default compiled or interpreted there obviously are use cases where default option is not the most suitable one. So if you want to write some system scripts in C, you probably better of using interpreter. Or if you happen to run a huge website with billions of page views (Congratulations on that by the way! :)) that happens to be written in PHP, well you might just feel a difference in performance if you compile your PHP.

Although the above paragraph may sound a bit like conclusion it doesnt end there. Because well interpreted and compiled is really just the the end-points of spectrum of different ways in which language is translated to binary CPU instructions. Lets take a look at Java. While it has a compiler, and you need to compile it every time you edit the source code. Can you really put it on the same shelf like C for example? Well not really… because Java is compiled to Java byte-code, which is then executed by Java Virtual Machine (essentially an interpreter). So while most of the time it refered to as compiled, you can see how there is subtle difference.

Lets take another example. There is quite a new language CoffeeScript. Now this one compiles to Javascript. Which then can be interpreted by any Javascript engine. So again while this is similar to how Java works I hope you agree that it is a bit different (compiles to another programming language, as opposed to bytecode).

I could continue the list but hopefully by now you see that it is not simply compiled and interpreted, there is options in between, each offering a unique set of advantages and disadvantages.

Follow Me