If you want to show off the power of your programming language, nothing works better than a cool one-liner (or even better, a code tweet).
I have to program in a few different languages, of varying degrees of power, and one thing you start to notice is the 0-liners — the code that just doesn’t exist.
An obvious example is how garbage collection (or ARC) gets rid of memory management. Here are a few more examples:
In Objective-C, nil sinks messages. This means that in a lot of cases, you don’t have to check for nil and “the right thing” happens automatically. If you send nil a message, it’s a no-op, and if you need a return, you get 0 for scalars, nil for objects, 0-filled structs, and undefined for anything else. You still consider the nil case, but you usually don’t need to write any code.
This is a real example of a language completely implementing a design-pattern, in this case Null-Object. You can get similar behavior by using this pattern. Clojure does even better by letting you implement a protocol on nil to provide implementations for its functions called with nil. But, neither of those are 0-liners.
I don’t use F# (or Scala), but my understanding is that when Some/None discriminated unions are used in computation expressions, the computation will end and return None if some part of the expression returns None. This is a classic 0-liner, if I’m right.
async in C#/F# (and go blocks in clojure’s core.async and go) rewrite sequential looking code to actually be asynchronous with callbacks. Miguel de Icaza covered this recently in his post, Callbacks as our Generation’s Go To Statement.
Just like in the Go To days, or the days of manual memory management, we are turning into glorified accountants. Check every code path for the proper state to be properly reset, updated, disposed, released.
In this case, only go and clojure are true 0-liners, as you still need the await keyword in C# (and let! etc in F#) to indicate a blocking call.
clojure’s BigInt contagiousness turns your overflow checks into 0-liners. If this is a possibility in your integer algorithms, it’s worse than null checks.
What’s your favorite 0-liner? The nice thing about them is that they don’t use up any of your 140 characters in a tweet.