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):

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:

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.

8 Responses to Code readability through conciseness

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.

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.

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’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.

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.

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.

About Me

I live in San Francisco and am the Co-Founder of Two Bit Labs where we develop iPhone, iPad, and Android mobile apps for our clients. I love the mix of team leadership and working as a hands-on contributor. My technical passions include Swift, Kotlin, Ruby, Cloud Computing, and open-source software.

I also love to sail and my wife, daughter, and I sailed out the Golden Gate in 2007 on our 38 foot Hans Christian cutter (sailboat) on a 3 year cruise. Read about it at http://sailsugata.com.