Why Clojure Is the Best Choice for Your Project? 4 Tech Reasons
august 27, 2019
6 min read
Clojure isn't a mainstream programming language. But Freshcode team regularly completes projects with this powerful and convenient solution. That's why I want to describe technical reasons in which Clojure development matters.
What is the final goal of creating any programming language? From my point of view, it aims to enable writing clear and effective code. Rich Hickey, the author of Clojure, wanted to make written programs simple. This helps to speed up the software development process, as well as reduce the time needed for code understanding, changing and support.
Clojure syntax can make a challenge for beginners. At the same time, it's easy and convenient for learning and coding. After getting used to this language, you will see the technical benefits, which are especially important for enterprise projects.
All Clojure advantages are based on 4 principles. Let's see how they are implemented in the language.
Clojure relates to the prominent and elegant Lisp family. One of its main properties is homoiconicity, which causes lively debate among software developers worldwide. According to Wikipedia:
Imagine a Lisp program, which is presented as a normal Lisp list. Meanwhile, you can manipulate it using other Lisp code. So, Clojure developer can access and transform code to data. This Lisp peculiarity shows the concept of "code as data".
This feature is conceptually possible because of the minimalist and utterly consistent Clojure syntax. The whole program code is represented via S-expressions (tree-like structures). They have just the same type as it is supported by Clojure Core. So, they can be easily transformed as data through macros.
The strong macro system gives an opportunity to create code that generates another code. It enables software developers to write extensions without waiting for the programming language designers to implement it. The latter is also relevant for writing own Domain-Specific Languages (DSLs) with the help of macros. They are responsible for"translation" from your own language to Clojure.
The benefits of well-designed DSLs are obvious. Clojure's macro system gives you excellent opportunities for using them.
Writing the internal DSL with these macros leads to a new level of code refactoring. It also expands the language itself with modern elements. They are useful for the implementation of business goals in the context of each specific project.
The core.async macro in Clojure is one of the best examples of this principle benefits. core.async brings the power of CSP-style concurrency to Clojure. Thanks to the macro, it was created as a library without interruption of Clojure language.
Compojure is another popular Clojure library that is based on the homoiconicity principle.
Look at the example:
Here you can see the macro "GET", which extends the language syntax. It provides a convenient interface for describing a series of "GET" endpoints. Their URL starts with /static/. The "location" parameter is also reverse here and validated with the help of regular expressions.
Look how we can transform this code:
An anonymous function-processor is created here. It is transmitted to the special function make-route, which puts the function-processor into the supportive middlewares.
It seems to be complex. But in reality, this Clojure abstraction provides nice coding experience and helps to make things referentially transparent. The macro above is needed for hiding the realization details from the developer. Of course, until he/she doesn't need them.
2 Functional approach
What causes complexity and problems in computer programs? Side effects are among the main factors of our challenges. You can't fully avoid them. But you can localize the spillovers and the programming language should help you.
As a functional language, Clojure encourages you to write pure functions. The results of such functions depend only on the input parameters. So, it doesn't matter how many times you start the function. The result is always the same. This simplifies testing because you don't have to try various queues and find the right state of inputs.
Pure functions are also handy for analyzing and refactoring. They are extremely simple, even if they make a great job.
3 Immutable data structures
Clojure has features of an object-oriented language. So, it initially includes a set of immutable (unchangeable) structures and methods for working with them.
The immutability is handy in writing multithreaded applications because it negates a whole class of bugs, related to synchronization of changing variables between threads. So, you can build the entire information models using the few immutable data structures included in Clojure. The predictable code is easier to write and easier to test, which helps to develop a product in short terms.
For example, look at the FreshCode case. The development of an e-commerce platform that supports more than 1 million players took just 4 months. It proves that Clojure allows programmers to focus on engineering efforts instead of extra details.
4 Multipurpose solution
Clojure is a good choice for a wide variety of projects. You can use it from social networking industry to Big Data solutions. Initially, Clojure language was targeted for working with JVM. So, the most popular modern Clojure implementation uses the Java Virtual Machine. This enables simple interaction with Java libraries, creation of Java objects and using the Maven repository. Java platform's maturity and the huge ecosystem provide many benefits for Clojure developers.
An interesting fact is that although JVM primarily runs Java, about 3% of JVM users are programming in Clojure. It's a remarkable fact, which shows the great potential of functional programming and Clojure itself.
Due to the same syntax, you can write frontend in ClojureScript and backend in Clojure (or ClojureScript in the case of Node.js application). What is more, sometimes the same code overlaps in frontend and backend. Clojure compiler allows you to put this code in *.cljc files and use it simultaneously on both sides.
More reasons to fall in love with Clojure
Except for the mentioned tech benefits, Clojure has several other strengths that make it a good solution for your product. Let's take a look at them.
My articles will cover other aspects of Clojure software development. So, stay in touch and enjoy the meaningful content by the Freshcode team! You can also share your experience in using different Clojure tools. I'm curious about your thoughts on writing code that matters.