Saturday, September 7, 2013

Coding 101: Types of Languages (Imperative and Declarative)

Let's push forward with Coding 101.  The topic for this segment is the different types of programming languages.  I mean for this to be a high level post and maybe in the future I'll go into more depth about them.  Anyways, let's get started.

First off are the languages that most people first get introduced to in computer science courses.  These languages are the 'imperative languages'.  Examples of this kind of language are languages like C, Java, Python, or Ruby.  The reason why these are called imperative languages are because each line of code you write is an instruction.  It's a command.  You can think of languages like these as recipes.  You tell the computer a set of instructions that have to be done in a specific order.  Logically, these kinds of languages are relatively simple to grasp once you understand the syntax that goes with the language, much like learning French or Mandarin.  Since things (almost always) happen exactly in the order that you write them, it's usually pretty simple to figure out when something goes wrong because there is an order that things happen in.  There are some wonky edge cases but as a whole this is true.

On the other side of the language fence are declarative programming languages.  Examples of this kind of language are Lisp, Haskell, Scheme, or parts of SQL (select queries in specific).  These languages differ in that the idea behind them is to declare what to do with your data.  For example, imagine you have a bushel of apples, some of them are red and the others are green.  Now, imagine you want to know how many red apples there are.  In an imperative language, you would have to take each apple out, examine if it's red or green and take a count.  In a functional programming language you would apply a function of whether something is red or green to the bushel and get the subset of red apples from which you can take the count.  Or you might just ask to get all the red apples from the bushel.  Internally, it's quite possible that the two approaches look the exact same, but from our perspective as coders they look quite different.

Let's take that example into further detail and begin to understand what these languages might look like.  For imperative languages you might go about the example in this manner:

for each apple in the bushel:
    if apple is red:
        apple count = apple count + 1;

print apple count;

It's relatively simple to see what the code is trying to do.  While that code would not compile in any actual language, it is quite similar to any of the languages I listed earlier.  But now let's take a look at the other case, declarative languages.  To do the same thing in SQL you would probably do something like this:

Select count() from bushel where color = red;

Obviously, you are getting the count of red colored apples from a bushel.  SQL in particular is of interest in that it is used as the main language for getting data out of databases.  SQL stands for Structured Query Language and just by seeing the acronym spelled out it becomes apparent that it is meant for looking things up.  However, the devilish part about SQL is that it cannot do all the things that an imperative language can.  It is quite poor at doing much more than retrieve data or simple analysis of data.

That doesn't mean that declarative languages are bad at those things, just that SQL isn't great at it although it is amazing at what it does do.

This post is actually going quite longer than I intended so I think I'll leave it here.  Next time I'll be talking about the difference between typed and untyped languages.  To give you a sense though, knowing SQL well can be really useful and lucrative (pay attention to the salary estimates on the side).  However, knowing just imperative languages like Java or C++ or Ruby or Python can also pay quite well.  If I were to restart all over, I'd learn Java and Python first.  The two of the are both imperative languages but differ just enough in syntax to trip you up. However, if you know those two, you can learn pretty much any other imperative programming language.  I hope this wasn't too boring.  Until next time.

--CsMiREK

No comments:

Post a Comment