My take on ‘Why Clojure?’

Both homoiconic nature and functional side make clojure very distinctive language. They are both inherent in making clojure what it is and i would not take one before the other.

As we have blog posts covering the reasons for FP here i’m going to take FP as a paradigm more or less granted in this post.

Why do i see clojure as a great language in the context of functional programming?

Learning and unlearning

First of all it’s hard to learn a programming paradigm in a language that does not embrace it. A wrong tool drives you to make wrong choices. It leaves you without actually learning the paradigm. It is by far the best thing to do to learn a paradigm using tools meant for it. Eric Normand wrote a good blog post about this problem.

To understand the benefits of FP, you have to use languages meant for it. Languages like Haskell (and by extension Elm and Purescript), Clojure, F#, Scala, Other lisps, Erlang, or OCaml.

I take it that you’re not interested in why not Haskell family, F# or OCaml, but i’ll anyhow go through that part. Most of these languages have very limited use in actual business right now. Currently Haskell and OCaml don’t have any place in programming for business. Which is a pity and no fault of the languages . Elm is currently used in front end and as such is still lacking in backend. Purescript seems to be the most versatile and available of these. It works well both in front and on node.js making it a viable language used in professional fullstack dev.

Scala then is a very solid language running on an widely accepted platform: the JVM. It is a very good all around language, there is no doubt about that. It also has a very good yet complex type system. System that can guide a good FP programmer to discover things about their domain. There are yet few things making it lacking in comparison to Clojure.

  1. Main problem in learning is unlearning. The familiar look of Scala drives people to not learn it. It’s easy to hack something with it but actually using Java with different syntax. Learning first Clojure leads to faster understanding of Scala itself. Adding to this rudimentaries of Haskell helps even more. Learn You a Haskell for Great Good! is a good resource for this.
  2. The unique way of thinking about problems and the language that describes them. This way of thinking is very valid and a good tool to learn for any programmer. Biggest reason for this is the homoiconic nature of the language. The great ability it brings in metaprogramming.
  3. Scala is currently only viable for backend use. Unlike scala Clojure has many supported platforms. It supports JVM, JS including node and browser, and CLR. All which acceptable platforms for business use. This makes it possible to share the same code in front end and backend. With it you can make a consistent whole. Creating front end components that are in sync with their microservice counterparts. In scala this is not possible as Scala.js is lacking in actual production grade stability.

So yes scala is an interesting language but not the best to learn FP. Especially so for Java developers.

Motivation as a driver for effectiveness

It’s actually quite hard to have valid measurements for coder efficiency. Yet one thing we do know. Study after study has found that developer motivation is elemental to productivity.

With unenthustiastic devs its best to inspire them. One of the main benefits comes from the fact that clojure is easy to use and makes coding actually fun again. This is a quote you do hear quite a lot from clojure devs.

As this is about inspiring people, you should go with what is good and nice and feels good and brings joy to you. As then you’ll be more inspiring.

Final words

It also goes down to the provable fact that Imperative Paradigm is more complex than simpler FP. Inside FP clojure is a very very very easy way to learn it. Much easier one to reap the rewards than most others.

If you have a bunch of coders struggling with imperative stuff. And you want to get the benefits of FP as fast as possible. Clojure is the fastest one to learn.

Why should I choose functional paradigm?

Choosing a functional paradigm language has been a hot topic for a while. Functional vs. Object Oriented Paradigm is a constant discussion topic within the developers.

Before thinking the benefits of FP, the working environment must be suitable for modern work

The key of success at the management point of view is not to micromanage. Let the team choose their working methods and tools. The project is always a compromise between time, features and given resources. It’s all about how the project management sees that triangle.

Every tool and way to work has their own place. Telling the developers HOW to do work instead of WHAT to do is always a problem. It kills creativeness and get the authority outside the dev team. Too often the author is someone who doesn’t know about the actual developer work.

Treat the professional developers with this Sledge Hammer principle: “Trust me, I know what I’m doing”. Developers should know exactly how the program works, because code is the most detailed specification of the solution. It’s all about communication between the people – and between the human and the computer.

When the environment is suitable for working, then we can discuss about the benefits of FP

So why should I choose FP instead of OOP/imperative? What benefits it would bring the table? Here’s some thinking I’ve done in the past years based on my experiences:

Functional programming makes especially list handling a way easier. There are effective functional languages like Clojure or Scala. Another option is to use functional libraries like Ramda or Lodash. I like flexible code, so I prefer using scalable languages, like ES6, Scala, Java8 or C#/LINQ. With OOP and FP combined I have more options available. It’s also a safe choice when there ain’t so much pure functional programmers in the team (yet). Scaling the Object Oriented code with functional flavour is understandable compromise with functional averages like me. Actually I’m on my way of using only functional languages.

Here’s some benefits I found on functional paradigm which could save some money

It’s stateless and then also immutable, so no side effects

Object Oriented Paradigm is all about sharing the lifecycle between the objects. OOP makes problem solving often too complex, and complexity costs. Mutable state makes side effects possible, so code is harder to test with a full coverage. Developing with FP forces to code small functions. Simplified syntax (especially with pure FP languages) gives more time to think the actual business logic. Focusing on one thing at the time is clever.

When the state is immutable, it’s way easier to scale up and still control the whole

When programming high capacity systems, it’s crucial to design the software architecture to be scalable for parallel processing. If the program isn’t handling any state inside the runtime, it’s way easier to scale by using micro services or FaaS container platforms like AWS or Azure. Then the runtime isn’t blocked to serve only one client for a long time – there’s more time to handle many short tasks instead of a one big blocker.
So no more if-else-if-else or switch-case spagetti, or recursive for loop hell. Only function chains like map, filter or reduce one-liners.
It might be hard to transform the brain in the right mode after coding years with OOP. Functional code readability might cause many WTFs in the beginning, but – trust me – It’s worth of it. Like Twelve-factor manifest says “Execute the app as one or more stateless processes“. Start by one and try to write more if it feels right and suitable for the context.
Learning functional programming makes you a better OOP developer. You don’t have to be an expert, but it’s beneficial to learn something new. It’s hard at the beginning, but with the supportive team it’s possible to achieve.
Functional languages are simple by design when you let it be so.

Here’s a recap of my points:

  • Let the developers to choose the right tools and processes for building the high quality solution
  • As a team member, encourage the colleagues at least try to code with FP
  • Functional code is simple and easy to maintain. when it works, it works (no side-effects like in OOP). Broken code is easier to recognise and refactoring is less risky operation
  • Data must persist outside the code (like in Redux architecture). It shortens or eliminates debugging marathons. Less risk for wasting the time hunting the bugs from the production environment

See also: