Why I Picked Clojure

DZone 's Guide to

Why I Picked Clojure

Learn more about why developers are choosing Clojure as their language of choice.

· Java Zone ·
Free Resource

Clojure is a functional programming language that runs on the JVM (Java Virtual Machine), CLR (Common Language Runtime), and JavaScript platforms.

I was considering learning Lisp for a long time. The reason for this is that my lecturer, Chris Stephenson, had strong discourses on functional programming and was using Racket (a Lisp dialect) in some of his classes. 

Because of that, I read and watched the experiences of different Lisp users. The most interesting part of my research was the approach users showed towards learning this language, acting as if it was a kind of secret space technology.

The experiences of Paul Graham with Lisp and the fact that he wrote on the Hacker News site with a Lisp dialect (Arc) aroused my curiosity.

I said: “Okay. Now, it is time to select a Lisp dialect.” I found the Clojure language among Lisp variants, and I saw that it can run on JVM. Moreover, it was a platform that I was familiar with as a Java developer. The comments about the community were also very positive, and so, I began to take the first steps.

I first began my Clojure journey in 2015. In this post, I will explain why I now do not to use any other languages (Java, JavaScript, etc.).

#1: Simplicity

The Clojure language has a very plain and simple structure; the syntax is also very plain. For example:

(+ 1 2)=> 3

Open the brackets, write the name of the function to be called using the parameters, and close the brackets — that’s all, and now, you know how to write Clojure. Let’s say the rest is just a few more details!

The first version of Lisp written in 1958 and used the same semantics as the current version (mostly). In other words, there is no syntax evolution on different versions of the language, such as Java (see Java 7/8). The core structure is very plain and simple.

#2: Java Interoperability

The creator of the language, Rich Hickey, has a very pragmatic and strategic reason for writing Clojure on the JVM. As it was written, Clojure gives access to all available Java (or any JVM language) libraries and their frameworks. So, you can call Java code from Clojure code or vice versa.

It should be considered that the languages written on the JVM have such a great heritage, and this is a huge advantage, especially for developing enterprise software.

In the following example, we check whether all of the String characters are in capital letters by using the  isUpperCase() method, which is inside of the  Character class in Java:

(every? #(Character/isUpperCase %) “HELLO”)=> true

#3: REPL (Read Eval Print Loop)

REPL is a secret weapon for Lisp programmers, and it is a tool that enables you to interactively contact with the program you write and evolve it rapidly.

Discourses of REPL-Driven Development are never-ending in the Clojure community, and it has become quite popular. The reason for this is that you can productively and quickly write a code. This progresses like finding a bug or adding/removing a feature can be easily carried out through quick feedbacks.

It is a dynamic language and there are very important advantages. For example, you built your web application, but then, you noticed an error in the message that will be shown on the screen and change it (or did a more complex change); Clojure quickly changes the Java bytecode and contains the change we desire. This will save SO much time.

If you have to make such a change via a language like Java, unfortunately, you have to restart the application (most of the time).

The biggest advantage of Clojure is that it can be both flexible and dynamic under a powerful system like the JVM. It is dynamic and multi-threaded (unlike Python and Ruby), while also running on the JVM — what else do you want? 

#4: Macro

After I learned the macro system of Lisp, I experienced real enlightenment. The macro system provides you with an opportunity to extend the language; you could add a feature into the language. In fact, a macro is the kind of function that produces function(s) for you at compile time.

In other words, you can get rid of duplicate codes that you don’t want by writing nice macros. For example, let’s suppose we have a nested function thread as follows:

(if-let [a 1]  (if-let [b 2]    (if-let [c 3]      (+ a b c))))=> 6

Now, as you well know, the code is not nice at all, but if I write a macro that will produce this code at the compile time with a more classy syntax, everything will be as I want.

Let’s write our macro called  if-let*:

(defmacro if-let*  ([bindings then]   `(if-let* ~bindings ~then nil))  ([bindings then else]   (if (seq bindings)     `(if-let [~(first bindings) ~(second bindings)]        (if-let* ~(vec (drop 2 bindings)) ~then ~else)        ~else)     then)))

I know it looks very complicated, but I would like to show a macro example. Now, the nested code will be produced for me, but I will only write the following part:

(if-let* [a 1          b 2          c 3]         (+ a b c))=> 6

Of course, there can be much more complex examples. I have the chance of decreasing code on a great scale and the honor of having a clearer code by using the macro in Clojure projects.

Java programmers know that the try-with-resources feature is available in Java 7 version. Therefore, IO classes that are written in  try() are automatically closed when their progress is finished. For example, a macro in Clojure called with-open settled this progress in the standard library.

Briefly, in Clojure, you don’t have to wait for minor changes in different versions as in other languages. We can add these features by writing cool macros, instead!

#5: Concurrency

The creator of the language worked mainly on concurrent systems for a long time and used a variety of languages, such as Java, C#, and C++, while working. In his interviews, he talked about the difficulties of trying to write concurrent systems via these mutable languages and said that the main reason for this problem originates from that they are mutable by default.

In fact, Rich Hickey said that he wrote Clojure for himself. He developed Clojure, which is immutable by default, for concurrent systems, because this work gets harder to be carried out due to kinds of difficulties (mutable data problems). Immutable data will not change when it is sent to another thread, so we don’t have to worry about the state of the data.

One of the biggest benefits of Clojure is that it was designed in a manner that it makes writing concurrent programs easy and entertaining, as well as provides an opportunity for fewer amounts of error.

Moreover, the language has four mutable reference types and this facilitates our work on state management issues.

Here are the four mutable reference types:

Var: Local scope

Atom: Atomic field (like AtomicBoolean in Java)

Agent: Async programming (similar to the Actor system in Scala)

Ref: STM (Software Transaction Memory) ensures that the state of more than one variable can be changed in a coordinated manner and there won’t be any condition like deadlock. It has a similar structure to the database transactions.

#6: ClojureScript

ClojureScript is a compiler for Clojure that targets JavaScript. So, you write Clojure code and finally have an optimized JavaScript code.

When you work on frontend and backend, you use a single language, and thus, you have the advantage of motivation and time. So, there is no mental context switch when you write React for frontend while writing Java on the server side.

Clojure and ClojureScript can share the same code (Reader Conditionals). Therefore, we write the code at once and use it on both frontend and server side (example usage: Validations).

I can say that these technologies provide nice opportunities for being a Full Stack developer.

#7: Community

Clojure community consists of really cute, helpful, and generally senior and experienced ladies and gentlemen. Of course, it is not as big as the community of other languages. It has a modest, small, and sympathetic volume because it is a niche technology ecosystem.

As far as I’ve seen at the Clojure meetings in Berlin, in which I constantly participate, I can say that the profile of Clojure users generally is a nice group of people consisting of chief scientists, mathematicians, and developers who have 10+ years of experience.

The Clojure page of Reddit is a quite active formation, and everybody easily asks whatever she/he wants and receives the answer.

#8: References

Here are some helpful references for you to learn Clojure step by step.

Final Thoughts

Clojure completely changed my perspective on programming. I found myself a more productive, efficient, and motivated developer than ever before.

The situation eventually arose where one Clojure programmer was able to carry out work that could be done by 2–3 Java programmers — you can ask other professional Clojure developers; they will agree with me!

I know two more developers who also develop Clojure in Turkey — Umut Gökbayrak and Üstün Özgür.

As far as I know, there are currently 5/6 people who develop projects with the “Clojure Developer” title in Turkey.

I hope this number will increase in Turkey in the near future because I think there is a need for diversity of programming languages in our community.

Those looking for open-source Clojure projects developed by me can check out my GitHub page.

Java, clojure, clojurescript, functional programming, lisp, programming language

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}