Code readability through conciseness

One of the things I love about newer languages like Ruby and Scala (and to a degree Python and Groovy) are the language features that allow you to dial conciseness up or down for readability. Take for instance the typical one liner for summing numbers in languages that support anonymous functions (which can be a bit cryptic in terms of readability):

Ruby
sum = [1,2,3,4].inject(0) {|result, value| result + value}
Scala
val sum = Array(1,2,3,4).foldLeft(0) ((result, value) => result + value)
Groovy
sum = [1,2,3,4].inject(0) { result, value -> result + value }
Python
sum = reduce(lambda result, value: result + value, [1,2,3,4])

One of the jobs of a good programmer is to make their code readable and in this case by naming the variable sum it’s obvious what’s going on to the right so if you can simplify the anonymous function or use syntactic sugar to reduce the amount of code there’s a good chance you can improve the readability as follows:

Ruby
sum = [1,2,3,4].inject(:+)
Scala can either reduce the anonymous function
val sum = Array(1,2,3,4).reduceLeft(_+_)
or in Scala 2.8 they added the sum method
val sum = Array(1,2,3,4).sum
Groovy can't reduce the anonymous function but has a sum method
sum = [1,2,3,4].sum
Python can't reduce the anonymous function but has a sum function
sum = sum([1,2,3,4])

Then even if the programmer reading the code is coming from another language and doesn’t know :+ or _+_ you’ll still have improved readability because they certainly understand how a sum is computed and can move past the line of code instead of having to read 3-4 lines of a for loop for something as simple as a summing operation. I love the balance of being very concise with the simple stuff so that I can be more verbose with the complex stuff, whereas in Java even the simple stuff is often extremely verbose.

There is of course the extreme of making your code too concise. Look no further than write-only languages like Perl to see where too much conciseness and obfuscation can lead to unmaintainable code.

This example is only trivial and somewhat contrived but I find that newer languages like Ruby and Scala give you significantly better tools to elegantly dial up or down the conciseness than Java or C# resulting in less code + more readable code = easily maintainable code.

As an example, I used just about every conciseness trick in the book I knew of when composing my submission for the 3rd Ruby Challenge’s Shortest Path Algorithm and I only discarded the ones that I thought obfuscated the intent. I like to think the reason my submission won was precisely because it was concise while still being readable. Writing that same algorithm with the same level of readability in Scala would be easy but in Java it would have tripled the lines of code hurting readability somewhat.

This entry was posted in Java, Ruby, Scala, Software Engineering. Bookmark the permalink.

8 Responses to Code readability through conciseness

  1. Mike Funk says:

    Todd, that sure is some cryptic looking code. But, then, there has always been an element of code obfuscators in the computer language community. I’m a minimalist, and I dig conciseness and readability – just not to the extreme you take it with Ruby and Scala.

    BTW, coming from a sailing family, very nice boat.

  2. Todd Huss says:

    Mike, if you haven’t used a language that supports anonymous functions and closures like Ruby, Scala, and soon Java 7 then the above would probably be very cryptic. I know it was for me when I first started using Ruby but now I’m a huge fan.

    I would still argue that summing an array with [1,2,3,4].inject(:+) is a lot more readable than the longer variant and a hell of a lot more readable than the non functional form using a for loop. Even if you don’t necessarily understand the underlying mechanics.

  3. steven says:

    cryptic code like this is exactly why i dont like these kind of dynamic languages. they try to get too smart for their own good and it just turns into a pissing contest just like this post.

  4. Todd Huss says:

    Steven, I can see your viewpoint and it’s definitely a matter of taste as to when you take advantage of the conciseness features of a certain language. If it’s the anonymous functions you’re not liking though, get ready, because anonymous functions and closures are coming in Java 7.

    Also, you can’t lump Scala with Ruby as being a dynamic language. Scala is statically typed and many argue that it’s type system is stronger than Java’s (especially around generics) yet with far less ceremony (where ceremony = having to unnecessarily re-declare your types over and over again).

    I do however agree that Scala syntax takes some getting used to.

  5. ic says:

    Then, of course, there is concision + clarity…

    PHP:
    $sum = array_sum(array(1,2,3,4));

  6. Todd Huss says:

    Ian, nice, highly readable and very concise!

    I’m not crazy about the procedural nature of PHP’s array_sum(…) and Python’s sum(…) but that’s just my personal taste for highly object oriented languages like Ruby, Scala, and Smalltalk versus hybrid languages like Perl, PHP, Python, and Java. I think there’s no question though that the Python and PHP examples are both highly readable and concise.

  7. Frederic says:

    You seem to put Java and C# at the same level. C# supports lambda, anonymous types, automatic type inferring, extension methods and so on… for quite some times.
    Someone coming from a C# background, will find Scala features sound quite familiar (if you omit the functional aspect).

    in C#:

    int sum = myArray.sum()

    If the method was not implemented, you could write an extension method.

    That said I agree that the language you are mentioning usually make it easier for writing elegant concise code and provide interesting constructs.

  8. Joseph says:

    I think a danger of using Scala is that each programmer in a group can choose their own variant of the syntax. It is widely regarded as being true that it’s an advantage for all of the code in a project to look the same. I would like to see something called maybe Scala II where the developers made a tight simple syntax where there was no wiggle room. Freedom is not a good thing. It is the responsibility of creators be authorities.

Comments are closed.