Home > Articles > Programming

Why You Should Be Using Clojure

  • Print
  • + Share This
Java can still work miracles, but after 20 years has accumulated some baggage and is moving slowly. Julian Gamble, author of Clojure Recipes, advocates a more modern option that takes advantage of what we've learned in the last two decades, combined with the rich heritage of functional programming. Clojure builds on a solid foundation to create code that's simple, powerful, and easy to reason about.
From the author of

From the author of

The combination of the Java language and the Java Virtual Machine (JVM) has been an enormous success, but the language, released in 1995, is now showing its age. Clojure is a breath of fresh air in that domain.

Clojure offers many useful modern features, including software transactional memory (STM), persistent immutable data structures, interactive programming through a read-eval-print loop (REPL), strong JVM integration by compiling to bytecode, simple Java API calls, and much more.

Clojure's syntax is based on S-expressions, which can be overwhelming at first, but some commenters have observed that the expressiveness of the language actually leads to fewer parentheses than are needed in Java. The power that this difference provides is a simple syntax, which can be transformed as data through macros. This is a programmable programming language, a property referred to as homoiconicity.

Clojure provides powerful tools for concurrency. In order to reason about sharing state between threads, three different approaches are used, based on refs, atoms, and agents.

The benefit of this functional design is that you can get a value synchronously, and you need not worry about any encapsulated behavior. Contrast this with the actor-based approaches popular in languages like Scala, where behavior is encapsulated in the actor. With an actor, the only way to get a value is via asynchronous message-passing. (Ultimately they solve different problems, but each language gravitates toward particular tools).

The author of Clojure, Rich Hickey, has taken the persistent data structures in Clojure to the next level with the introduction of the NoSQL database Datomic. In a sense, it's like a giant persistent data structure, allowing point-in-time rollbacks (for example, query against the state of the database before all the transactions of the last two hours). This feature provides detailed auditability for financial institutions, and it's great for quality assurance of applications because it makes complex application bugs easier to reproduce.

One of the distinctive benefits of Clojure is its extensibility; it relies on composition as its extension mechanism. This is shown in the recent library release core.async, which is a queuing paradigm; it enables communication between concurrent sequential processes. This is similar to async and await, which in C# are implemented as compiler extensions. It's true that the implementation of core.async in Clojure involves a deep macro that walks the code passed to it and then rewrites that code. The advantage of using macros in Clojure for a language feature change is that no changes to the core Clojure compiler were required. Language features that require compiler changes in other languages are mere libraries in Clojure.

Clojure is a real-world language that's production-ready. It's currently in use in production in financial institutions such as Citibank, Simple (formerly BankSimple), and Pico Quantitative Trading; and in health institutions like Max Planck and HigieBus. It's also in use at Amazon, Netflix, Groupon, and many other large organizations.

You can use Clojure in your day job starting right now. You'll need to explore some simple examples that you can use in your projects, to help build your understanding of the language. Take a look at Clojure Recipes for self-contained examples that show you how Clojure hangs together, so you can see the big idea.

Julian Gamble is a software engineer who has worked in the financial services industry for more than a decade. When he's not enabling billions of dollars to orbit the globe, he writes and presents about all things software-related on his blog.

  • + Share This
  • 🔖 Save To Your Account