Topic of the week: Readable code

readable-codeWhile researching for last week topic of the week, code comment, I stumbled upon this week topic “readable code”. The topic itself is interesting because some developer argues that code comment should be extinct and replaced by readable code. Is this possible ?

Well, I would argue that readable code actually depends on the programming language being used. There is a good example over at Simple Programmer on this :

Readability constraints

Just like you are limited to the elegance with which you can express thoughts and ideas using the vocabulary and structure of an early reader book, you are also limited in the same way by both the programming language in which you program in and the context in which you program it.

This is better seen in an example though. Let’s look at some assembly language.

.model small
.stack 100h
msg     db      'Hello world!$'
        mov     ah, 09h   ; Display the message
        lea     dx, msg
        mov     ax, 4C00h  ; Terminate the executable
end start

This assembly code will print “Hello World!” to the screen in DOS.

With x86 assembly language, the vocabulary and grammar of the language is quite limited.  It isn’t easy to express complex code in the language and make it readable.

There is an upper limit on the readability of x86 assembly language, no matter how good of a programmer you are.

Now let’s look at Hello World in C#.

      System.Console.WriteLine("Hello, World!");

It’s not a straight across the board comparison, because this version is using .NET framework in addition to the C# language, but for the purposes of this post we’ll consider C# to include the base class libraries as well.

The point though, is that with C#’s much larger vocabulary and more complicated grammar, comes the ability to express more complex ideas in a more succinct and readable way.

Want to know why Ruby got so popular for a while?  Here is Hello World in Ruby.

puts "Hello, world"

That’s it, pretty small.

I’m not a huge fan of Ruby myself, but if you understand the large vocabulary and grammar structure of the Ruby language, you’ll find that you can express things very clearly in the language.

Now, I realize I am not comparing apples to apples here and that Hello World is hardly a good representation of a programming language’s vocabulary or grammar.

My point is, the larger the vocabulary you have, the more succinctly ideas can be expressed, thus making them more readable, BUT only to those who have a mastery of that vocabulary and grammar.

Source: Simple Programmer

As stated above, there is such a thing such as readability constraints. It all depends on how rich a vocabulary in the programming language. Normally, the higher level the language is, the more vocabulary it have. This is because high level language are closer to human language, which in most cases is English.

But, there is a parsing overhead that slows down the processing speed of the programming language. Whereas low level language is less rich in vocabulary but have faster execution time because it is closer to machine code.

There are still a lot to talk about readable code, so shall we begin ?