Very High Level Language – So what?

When I first heard about Python I thought this is just great. I have had to listen to countless conversations about Java vs. C# and now we have to go through it all again with Python thrown in. I know Python is older than Java, but I only came across it after C++, Java etc. Python and Ruby are described as being Very High Level Languages (VHLL). When I heard that term for the first time I thought here comes another meaningless, yet ubiquitously used acronym. At first I did not pay much attention to these languages. I was very familiar with Java, C# and PHP. I figured Ruby and Python could not be too different from these languages. And sure, I may have been caught up in some Microsoft sales propaganda…

As with many developers, it was Rails that pushed me into learning Ruby. From the start, I really enjoyed Ruby – and still do. Dynamic scripting is a real eye-opener! Although I was enjoying this new dynamic paradigm, I still did not see much value in calling it a Very High Level Language.

This all changed at SPA. I attended “Agile Development for the Web and Elsewhere: A Tutorial in Python” presented by Nick Efford. In my opinion, this was the best session at SPA. Well organised and the content was spot on. It was a 6 hour session and Nick made many valuable points. He also explained the VHLL concept in great way. This was done with the use of this simple and effective slide.

Mental-model

This is when I had my first real VHLL a-ha! moment. Nick had placed Python and Ruby in a different class to Java and C#! I had realised that these languages were dynamically typed while Java and C# are (mostly) statically typed. I knew I was not comparing apples with apples, but I thought I was comparing apples with oranges. It turns out that I was comparing apples with apple pie.

When coding in C# you do not need to worry about the low level details that a C programmer needs to be concerned about. Similarly, when programming in Ruby you should not be concerned about the low level details that a C# programmer needs to worry about. Most of us are aware of the low level details a C programmer is faced with e.g. garbage collection. Now what low level details are there at the C#\java level? We can explore this with a simple example.

Let me explain with my poorly contrived example.  If you don’t understand the English, just skip to the code. This example is in the context of an on-line checkout, we are calculating the final or total cost of a purchase. The business rules are as follows:

Given a user’s sum cost of items and a minimum charge
When the sum cost of items is greater than the minimum charge
Then the total cost is the sum cost of the items

And

Given a user’s sum cost of items and a minimum charge
When the sum cost of items is less than the minimum charge
Then the total cost is the minimum charge

In C# you might implement it like this:

private int GetFinalCharge(int itemsAmount, int minimumCharge)
{
if (minimumCharge < itemsAmount) return itemsAmount; return minimumCharge; } [/sourcecode]

Or you may write:

private int GetFinalCharge(int itemsAmount, int minimumCharge)
{
return minimumCharge < itemsAmount ? purchaseAmount : minimumCharge; } [/sourcecode] In Ruby we could easily do this in one simple line therefore it may not be necessary to write a function. We would only use a function if we found ourselves breaking the DRY (don't repeat yourself) rule. A 'verbose' Ruby version: [sourcecode language='ruby'] def total_cost (items_amount, min_charge) [items_amount, min_charge].max end [/sourcecode] In the Ruby example we don't need to worry about finding the maximum amount, we simply ask Ruby to give us the maximum. We don't worry about the actual implementation. That is considered as a low level detail in a VHLL. I come across this all the time when writing Ruby. Ruby has a for loop, but thanks to the smart (or should I say sensible) iterators the for loop is practically extinct. Now imagine we had to find the maximum of three numbers, then our C# implementation would expand even more, we may even make use of LINQ. The Ruby version would remain virtually the same. These methods would be more scalable if they took an array as input. In that case the C# version would certainly grow, maybe not in size but certainly in complexity. The Ruby method is so simple, we could still consider dropping the method and simply dong it in the calling code. Look how clean Nick Effords Python example is: Example-of-python
I am sure you can easily tell what that program does. It is so clear and simple. This is achieved by small features e.g. no { } required, uncluttered object instantiations etc. All of these small niceties add up to clean and easy to read code.  This allows the developer to focus on the bigger, domain specific issues.

I do not mean to perpetuate the myth that code which looks like English is easier to read and understand. I am saying that clean, uncluttered code is easier to read.

In conclusion, when developing in a VHLL expect more from the language. Use the features that allow you to focus on the business domain and not the low level implementation details. If you are writing a for loop or a similar construct, you are probably doing something wrong. There is a reason these languages are called Very High Level Languages.

Final Note: Ruby and Python are two examples of Very High Level Languages, there are others. I restricted this post to Ruby and Python examples as I did not want to end up in trouble – I am perticularly wary of the SmallTalkers.

Advertisements

15 thoughts on “Very High Level Language – So what?

  1. Nice post Daryn. As you know I enjoy using High Level Languages.

    I’m not sure that you’d call Nick’s example an illustration of a *very* high level approach as he’s telling the computer a lot of how it should do what he wants.

    I don’t want to suggest a more prose like approach either, but to be a VHLL I reckon it should be very declarative. Granted “High Level” is a relative concept, but of course I’m just splitting hairs as an excuse to make a comment on a VHLB (very high level blog, in terms of traffic and content) like yours.

  2. Let me just place Nicks code into the context in which it was delivered. At this stage in his session Nick was still introducing Python. I am assuming that he wanted to ease us into the language and therefore he used Python in a way that would be familiar to a Java developer. I have no doubt that Nick could have provided us with a more pythonic implementation. I believe he chose not to as it may have been inappropriate at that stage of the session.

    I included the example in this post to show that even if you are not making full use of the VHLL you can still produce code that is uncluttered, easy to read and easy to understand.

    I should have made these points clear in the post.
    My apologies Nick.

  3. I agree with you Adam (ashimali), this code could be more declarative. As I said in the post, one should think twice when writing a for loop or a similar construct in a VHLL. In the lottery example the while loop triggers little alarm bells. I have some ideas on how we could make this code more declarative. Before sharing those ideas I would like to hear from the rest of you. How would you alter this code to be more ‘proper’? Examples in any VHLL are welcome…

  4. Certainly it’s a clean enough example which mimics what the lottery machine does. You could improve it slightly by making numbers a set. Personally I’d exit the loop when len(chosen) == 6, since you have a more precise exit condition.

    A better solution uses the batteries Python includes. This is a job for random.sample()

    import random
    
    lottery_result = random.sample(range(1, 50), 7) 
    bonus_ball = lottery_result.pop()
    
    print "This week's numbers are", sorted(lottery_result)
    print "The bonus ball is", bonus_ball
    

    Incidentally, I don’t like the “Very” in very high level languages. I prefer to simply use the term “high level languages”, and make it clear that C++, Java etc are no longer high level.

    http://wordaligned.org/articles/pitching-python-in-three-syllables

  5. And yes, I realise I’m commenting on a presentation you’re commenting on! I just hope Nick Efford went on to show how to use random.sample() to simplify the code.

  6. @Thomas: you’re right that a set would be slightly better from a semantic standpoint, although the code wouldn’t look quite so simple. I didn’t use random.sample in this specific example because one of my aims was to introduce the idea that block structure is indicated by indentation.

    I’d agree with the point that “very” is perhaps slightly misleading in this context, but personally I’m going to keep on using it to describe Python for as long as folk continue to describe Java and C# as high-level 🙂

  7. @Nick: Thanks for the comment. And for the session at SPA!

    I would like to just remind all of you that I have placed Nicks example out of context. At this stage he was introducing Python and he was not explicitly explaining high level coding. Although I am encouraging you to improve Nicks example, this should not reflect negatively on Nick as a developer or as a conference session leader.

    I also prefer to use the ‘Very’ just to differentiate it from Java/C# which are categorised as High Level Languages. The purpose of this post was to advise developers to change their mindsets when coding in Python, Ruby etc.

    @Thomas: That is a great declarative example! That is how to use a VHLL (or should I say HLL?). It would be great to have a Ruby implementation. Any takers?

  8. Daryn, Nick, points taken — I wasn’t at the presentation so I’m commenting on a slide out of context. I see now that you’re really talking about code layout. Sometimes it’s hard to find good examples in Python because the libraries do so much for you!

    Nick, re VHLL, my point is that we should appropriate the term HLL for Python etc *because* it’s a term Java and C++ have traditionally claimed their own.

    Random sample is an important algorithm. It’s implemented in C++ and Python, but not C. I too would be interested to know if it’s there in e.g. Java, Ruby.

  9. @Thomas: I am really impressed by your implementation of the lottery example. That is using Python like it is meant to be used…

  10. Great post, I’m an I.T. student at The University of Queensland in Australia, I had a course about Operating Systems and Networks which introduced us to C. I had learned quite a bit about Java a semester ago so C was challenging but fun for me.

    I didn’t think anything could be more “direct” than Java, I’m convinced now that Java is “mid level” and that I should look into Ruby for the future.

    By they way I found your blog while looking for a Ruby on Rails tutorial.

  11. Hi, aussiezilian. Ruby is defiantly worth learning! It is so declarative, so inherently tidy.

    Did you find the rails tutorial on my blog? In case you did not, you can find it here.

  12. Pingback: O poder do Ruby « Guilherme Garnier

  13. Tested with Ruby 1.9:

      require 'facets/random'
    
      numbers = (1..50).to_a
      chosen = 7.times.map { numbers.pick! }.sort
    
      puts "This week's numbers are #{chosen}"
      puts "The bonus ball is #{numbers.pick}"
    
  14. Or better yet…
    This one works with Ruby 1.8/1.9
    rand_subset return values are exclusive of each other

      require 'facets/random'
    
      numbers = (1..50).to_a
    
      puts "This week's numbers are  #{numbers.rand_subset(7).join(', ')}"
      puts "The bonus ball is #{numbers.pick}"
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s