Java with a Clojure mindset
Tags: clojure, java, talks
Published: 2019-11-27 13:47:14

---
title: Java with a Clojure mindset
tags: Clojure,Java,Talks
description: Video, transcript and slides from the Java2Days and Oredev 2018 talk
published: true
cover_image: https://danlebrero.com/images/blog/java-with-a-clojure-mindset/040-what-about-parenthesis.jpg
canonical_url: https://danlebrero.com/2019/03/06/java-with-a-clojure-mindset/
---

In this talk, using a real application as an example, we will learn how to build and design Java applications that follow Clojure's functional principles using just core Java, without any libraries, lambdas, streams or weird syntax; and we will see what benefits those functional principles can bring.

A teaser first:

{% youtube 5iDL_lVcXM4 %}


Here are the slides in [keynote format](https://danlebrero.com/slides/java-with-a-Clojure-mindset.key), [ppt format (untested!)](https://danlebrero.com/slides/java-with-a-clojure-mindset.pptx) and [SlideShare (somehow broken!)](https://www.slideshare.net/DanielLebrero2/java-with-a-clojure-mindset).

And the video:

{% youtube u2CT3o09JhU %}


Feedback and questions welcomed!

*****

# Transcript

Lets start with a quizz. Who can tell me what is this?

![alien](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/001-alien.jpg "Alien")

This is how Clojure looks like the first time that you see it.

And I want to point out 3 important things in this picture. 

First, it is very ugly, some people would say that it is even disgusting.

Second, there are a lot of things that make no sense at all. For example, why would you want to keep a bug in your mouth?

![alien](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/004-alien.jpg "Alien")

Or what the hell is that and what it is doing there?

![alien](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/005-alien.jpg "Alien")


But last, and most importantly, it is alien. And when we look at that picture, we are looking at it from our human point of view, from our Java perspective.

So maybe we are not being fair, and with enough time, 

![hypotoad](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/006-hypotoad.gif "hypotoad")

and the help of the hypotoad, maybe that picture would look like this:

![alien](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/007-alien.jpg "alien")

because with understanding, it will come a change on perspective, that will allow us to make a more fair judgement. 

And I know what you are thinking, Wonderwoman is not technically an alien and if we are talking about Clojure, the picture is missing 

![alien](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/008-alien.jpg "alien")

some parenthesis. 

But still, if we spend sometime studying these aliens, maybe we will find some superpowers that we can use in our day to day job as Java developers.
 
# Clojure

So what is Clojure? Clojure is a Functional, Hosted, Dynamic and strongly typed, lisp for the JVM.

![clojure](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/010-clojure.jpg "Clojure")

So what we are going to do in this session is, using a real world application as an example, see how Clojure affected the way that we built, the way we designed this Java application, how Clojure infected our Java code.

To give a little bit more context, when I got involved with this application, 

![wikus](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/011-wikus.jpg "Wikus")

I had already been a Java developer for 12 years, and a Clojure one for 3, so when I got involved I had reached Wikus Stage 2 on my journey, on my transformation, to a full blown alien mind.

The application that we are going to be talking about, is one of this bonus systems that you find in some betting industries. 

![bonus](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/012-bonus.jpg "Bonus")

Those of the kind of “if you put some money in your account, we will give you double that amount for free” or “if you join us now, we will give you a 1000 euros free cash!”. 

Now nobody is going to give you any free cash, so if you read the T&C, to be eligible to withdraw that “free” cash, to be able to get that cash and put it in your pocket, you had to first play or bet, a number of times in the system, or do a number of activities.

Also, in our particular case, and I am not sure if this is common, the client had a limited amount of time to place all these bets.

So removing all the marketing fluff, what our application had to do was something like this: 

> add some amount of money to the client account, if they had sign up for the bonus and they had made loads and loads of bets, within some timeframe. 

So from an implementation point of view, what the system had to do is know which bonus where available, which clients had sign up for one of those bonus, and then keep track of the interesting activities of those clients, which in our case would be making a bet and placing a deposit.

# Functional vs OO 

So lets start with the first of the differences: Functional vs Object Oriented.

Of the many principles, concepts and techniques that are usually associated with FP, I just want to focus on one that, in my personal experience, has made the biggest impact on the way that I design applications. And I think it is specially important because it is easy to translate, to use it most languages.

## Pure Functions

This is the concept of pure functions.

Pure functions are pieces of code that for a given input, they are always always always going to return the same result. Always. So same input, same output.

And why are pure functions so important? Pure functions have multiple benefits, but the key one is that pure functions are easier to reason about, because all the code within a pure function just depends on the input parameters. So the context that you need to keep on your head to understand a pure functions is small.

Pure functions are like physic laws, 

![laws](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/013-laws.2.gif "Laws")

because you know how a pure functions is going to work and you can rely on it working the same every single time.

And as pure functions are easier to understand, it also means that they are easier to change. 

And change is what we, developers, do for a living. 

> Programmers are constantly in maintenance mode. 
<cite>The Pragmatic Programmer</cite>

We very rarely write a new piece of code. Most of the time, we are just making changes on existing systems. Even if you created your project 15 minutes ago, you are already in the business if changing existing code. 

## Side effects

And when talking about pure functions, we have to talk about its evil twin, side effects.

Side effects is what makes your code hard to understand, because suddenly, to understand a piece of code, it is not enough to look at that code. Now you also need to understand all its dependencies, and all the libraries that are being used, and all the possible states in the database, and all the possible states of the network, and all the possible things that multiple threads could be doing at the same time.

The context that you need to keep in your head is huge.

Once you have side effects, 

![side effects](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/014-side-effects.gif "Side effects")

you are not sure what is going to happen when you make a change.

Well, that was a bit side effect. And this happens to us all the time. You make a small, and what you think is an innocuous change, on one side of your application and then suddenly a completely unrelated feature in the other side breaks without you ever noticing.

So for me, a key insight on functional programming, is that side effects are the enemy. 

![015-side-effects-enemy](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/015-side-effects-enemy.jpg "Side effects enemy")

So functional programming is about fighting and controlling side effects.

There are basically two kinds of side effects:

![Side effect types](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/016-side-effects-types.jpg "Side effects types")

1. Those side effects that change the state of your application
2. And then we have IO side effects. Of the IO side effects, I want to distinguish the Input side effects, sometimes called co-effects from the output side effects, called simply effects.

### State

So let's start by talking about how we can fight state.

In the case of the bonus system, our application has to keep track of the state of each client and its progress on the bonus campaign. 

To do that you can image that the application kept a map, with the client id as the key and some kind of ClientBonus object as the value. This ClientBonus object itself could have maybe Client object and a bonus object. Also, it will also have to keep track of the deposits made, so it could have a DepositList, which will contain a bunch of Deposit objects, and each of these deposits objects could contain even more objects. In a similar way, it would need to keep track of the bets done by the client.

So these would be our object graph, and we would have one of these graphs per client.

Now when it came to state management, I would traditionally made each object responsible for its own state change. So the Map would get its own little machinery, its own little piece of code to manage its internal state and to make sure that if several threads try to manipulate or read the map, they will see a consistent state.

![State management in Object Oriented](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/017-state-management-in-object-oriented.gif "State management in OO")

In a similar way, the ClientBonus object would also be responsible to manipulate its own state internal state and to provide a consistent view of it, and the same for each and every object. 

Now, from a complexity point of view, what does it mean that each object gets its own machinery to control its state? Each of those little machines are possible side effects that you have to take into account whenever you make a change to your code. You not only you have to think about the business logic that you have to write, you also have to thinking about any timing issues due to concurrent access to the objects.

So the code is mixing in the same place business rules and concurrency rules.

So what Clojure teach us is that, to simplify this, what you want to do is separate application logic, your business logic, from any state management, so you don’t need to think about both at the same time.

So how we do this? First by making everything immutable, everything is a value, even that map holding all of our ClientBonus objects. Now that everything is immutable, when writing your application logic, you do not need to think about timing, you do not care about what other threads are doing at the same time, because none of them can change to your object graph. So this frees your mind, this makes writing your application logic a lot simpler.

For the state management part, Clojure comes with a construct or a class, called Atom, which is basically the same as a Java AtomicReference, but with a little bit more functionality. 

> atom =~ j.u.c.a.AtomicReference

Lets see how an atom works.

An atom holds a reference to the whole immutable value, to the whole state, and your job as a developer is to write a function, that takes the current state as a parameter, and it produces the new state, and the atom machinery will make sure that the transition from one state to the next is done in an atomic way.

To understand it better, lets see what happens if two threads try to modify that state at the same time.

![State management Atom](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/018-state-management-atom.gif "State management in Atom")

Both threads will get the initial state and will start calculating the next state. Lets say that thread-1 finishes before thread-2. At this point thread-1 tries to change the state of the atom, to the new green value. To do that it tells the atom to do an atomic compare and swap operation. As the value used to calculate the green state is still the white value, the atom changes it state to the green value.

Now thread-2 finishes, when it tries to change the state of the atom, the compare and swap operation fails, because the atom is not longer pointing to the white state. So the thread-2 has to start again, but this time with the green value.

All this machinery about retrying and detecting conflicts is provided by the atom, so you as a developer only have to write a pure function.

Now to make sure that we all in the same page, I want to point out two things.

The first one is that the only valid values of the atom during the time were the white, green and red ones. Nobody ever saw the blue value.

The other thing is that if for example there was another thread, thread-3, that at time 0 read the current state of the atom and it keeps a reference to it for some time, as time moved, from t0 to t1 and t2, the thread-3 will still see the initial state, the white state. Because the value is immutable, nobody can touch it, which means that thread-3 can potentially be working with a stale value.

Now you may be wondering, wow, if I have to create a whole new graph every time that I want to change anything, isn't all this immutability going to be extremely slow, extremely expensive? And in practice it is actually slower, but not as much as you would expect. 

Lets say that you have this state, 

![Object graph](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/019-object-graph.jpg "Object graph")

and to calculate the new state, you need to change some field in that bet object over there. Of course, you cannot change anything, so you create a new bet object. As the bet belongs to a BetList, and again everything is immutable, it also means that you have to create a new BetList, which also means that you have to create a new ClientBonus, and a new bucket on the hashmap.

![Structural sharing](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/020-structural-sharing.jpg "Structural sharing")

Those 4 things are what it is different between the green and the white state. So to build the green state you just need to create 4 new objects and you can reuse all the others, and you can do this because those objects are all immutable, and we know that it is safe to share immutable objects. This technique is call structural sharing.

Now, this is still slower that mutating one field in the Bet object, but the cost is still very cheap, specially if we compare it with the benefits of this approach.

> Is this thread safe?
<cite>Every Java developer, every day.</cite>

Have you ever ask yourself this question? With immutability and atoms, you still ask this question, but the rules to answer it are a lot simpler as they don’t involve the Java Memory Model and the “happens-before” semantics.

![Thread safe rules](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/021-thread-safe.jpg "Thread safe rules")

The first rule is that the state within an atom is always consistent, as everything is immutable, it is not possible to see half-baked states. This already removes a lot of the complexity in your code.

The second rule is that the function to calculate the new state must be pure, as it maybe run multiple times. 

As we saw in the example with Thread-3, any decision that you make outside this pure function could be done with stale data or be subject of a race condition.

From this third rule, it should also be obvious that if you code has to look at two atoms to make some decision, the decision is not atomic.

So how all of this affected our Java code?

The first, obvious things, is that all the domain classes are immutable, so all the fields, including any map or list.

```java
public class ClientBonus {

    private final Client client;
    private final Bonus bonus;
    private final DepositList deposits;

…
```

Now for the state management part, we actually did not use an AtomicReference to store the whole map with all the ClientBonus.

In our case, as what one client does, do not affect the outcome of another's client bonus, our application logic just really needs the ClientBonus to be consistent, it doesn't need a consistent view of all current ClientBonuses. 

So what we did was to actually use a ConcurrentHashMap to hold the state, and then each value will get its own little machinery to advance time.

![Concurrent HashMap](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/022-concurrent-hashmap.jpg "Concurrent hashmap")

That machinery is provided by the compute family of methods of the ConcurrentMap, which basically provide the same semantics as Clojure’s atom, but on a per key level.

```java
public interface ConcurrentMap<…> extends Map<…> {

V compute(K key, BiFunction<…> remappingFunction) 
V computeIfAbsent(K key, Function<…> mappingFunction) 
V computeIfPresent(K key, BiFunction<…> remappingFunction) 
…
}
```

So this is how the class that hold the state would look like. 

```java
public class TheStateHolder {
    private final Map<Long, ClientBonus> state = new ConcurrentHashMap<>();
    public ClientBonus nextState(Long client, Bet bet) {
        return state.computeIfPresent(
                client,
                (k, currentState) -> currentState.nextState(bet));
    }
```

It contains the ConcurrentHashMap, and every time that the application gets new data, it just calculates the new state in an atomic way.

In our case, we decided that the ClientBonus itself should be the one that calculates the new state, 

```java
public class ClientBonus {
...
    public ClientBonus nextState(Bet bet) {
        ...
    }
…
```

so that nextState function must be a pure function.

So this way we managed to separate state management from application logic.

### Effects

So now that we know how to fight State, let's look at what we can do with effects. 

Effects are actions that our application has to do in order to change the state of the external world.

In our case, those effects were things like sending a notification to the user about the progress of the bonus, or paying the price into the client’s account.

Clojure, the same as Java, is not a pure language like Haskel, so it actually does not provide any special tools to deal with IO. So lets see how we can handle effects.

Typically, in our applications, we would have something like this. Some kind of *service* object that depends on some interface and then at runtime we inject some dependencies. 

![Service design](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/023-service-design.jpg "Service design")

If you think about what this kind of service looks like, you will notice that is it takes care of two things: it decides which side effects our applications has to do, plus it has to execute those side effects and deal with any possible errors or exceptions raised by doing the effects. So when you write the code for the service, you have to keep in mind both things.

So if we want to follow a more functional approach, we want to separate those two things, so we can work on them independently. On one hand, we want to decide what effects need to be performed, and in the other hand we have to deal with the messy and ugly details of interacting with the external world.

To decide what effects need to be performed, in our business logic, we can instead of calculating the next state, we can also calculate the effects. With this, calculating the effects to be performed becomes part of our pure business logic, becomes part of our pure function. 

```java
public class ClientBonus {
...
    public Pair<ClientBonus,Effects> next(Bet bet) {
        ...
    }
…
```

Note that this also means that our effects become explicit first-class concepts in our application.

![Notify class](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/024-notify-class.jpg "Notify class")

This would be an example of a class representing the effect of notifying a client about the progress of a bonus.

Now, even if another part of the system is going to be executing this effect and dealing with errors, our business logic can still decide on how the effects and the errors are going to be handle.

For example, in our business logic, we could be wrapping this effect in a Ignore error policy, while in other cases, maybe it could decide that the correct policy is to stop the JVM. 

![Wrapped in policy](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/025-notify-wrapped.jpg "Notify wrapped in policy")

Apart from the error policy, the application logic can also decide if the effects must be run sequentially, so if one fails, the rest will to be not executed.

![Sequential execution](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/026-sequential-execution.jpg "Sequential execution")

or that maybe the effects are independent, so that an error in one should not affect the others, which also could mean that the effects can be executed in parallel.

For our bonus application, we decide to not build any of these flexibility in the effects, as we thought it was not necessary, and instead we went to have a very rigid, very static, way of defining the effects, and encode in the type system what were the valid and possible chain of effects. 

But now that we have a description of what side effects must be run, we still need to execute them, we still need to run them, so some piece of code needs to go and interpret this description of the chain of effects. 

In our case, as the structure of this description was very rigid, we opted to just let each effect know how to run itself.

```java
public interface Effect {
    void run(AllDependencies dependencies);
}
```

Note that it is here were we pass all the dependencies required to execute those side effects, things like the http or JMS clients. 

Passing that AllDependencies object around has the benefit that it makes very obvious which methods are impure, because to be able to perform any side effect, the method would need to declare that dependencies object as a parameter.

It has the drawback that sometimes it is a little bit cumbersome to pass it around, and that the AllDependencies class is quite ugly, as it has to hold, and make accessible, a lot of dependencies. That AllDependencies class feel almost like your Spring Context.

So this is how our code would look like:
 
```java
Pair<ClientBonus, Effects> pair = theStateHolder.nextState(bet);
pair.effects.run(dependencies);
``` 

We calculate the next state and the effects to perform, and then we will perform those effects.

But the question is, is this thread safe? 

With the rules that we saw before regarding atoms, it is obvious that it is not, because one of the rules was that anything that happens outside that pure function that calculates the new state, could be subject of a race condition.

And were is the race condition here?

So let's say that two threads come and calculate the new state and the effects to perform. 

![Race condition](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/028-race-condition.jpg "Race condition")

The atom takes care that those two threads make a decision that is atomic, consistent and isolated. So far, so good.

But now that we are outside the atom mechanism, we are subject to possible race conditions, so it could be that the thread-2 performs its side effects before thread-1. 

![Race condition](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/029-race-condition-2.jpg "Race condition")

And this may, or may not, be acceptable, depending on your business requirements.
                                                                                            
So, atoms do not make all race conditions go away, but they should make it a lot more obvious of when they could happen.
                                                                                            
If in your application this kind of race condition is not acceptable, a possible option could be to use an Agent. 

Clojure Agents are basically like an atom, but it gives you the additional guarantee of being single threaded. If you are familiar with actors, they are a little bit like actors, in their concurrency model.

For our bonus application, this kind of race condition was not acceptable, but we decided to not use Agents, and stick with atoms, why?

Well, we were running multiple instances of the bonus service, so we are now in the realm of distributed system programming. 

As the side-effects that our application needed to perform, could not be done in an atomic way, the distributed system theory tell you that you must chose between at-least-once or at-most-once semantics.

In our case, we look at each effect and for each one, we decided what was more appropriate. For those that were at-least-once, we did no coordination, nothing. 

For the ones that required at-most-once semantics, we used a relational database as the coordination mechanism, as the DB provides ACID guarantees. 

![Race condition DB fix](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/030-race-condition-effects.gif "Race condition DB fix")

So before doing one an effect that required at-most-one semantics, the application will check with the DB, so that if there was a race between several instances to perform the same effect, only one will get the go ahead and execute it.

Note that it was still our pure functions, the ones calculating the effects, 

![At most once](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/031-wrap-at-most-once.jpg "At most once")

the ones that would decide when and which effects needed at-least-once or at-most-once semantics, and we did by wrapping the effects in a “at-most-once” policy.

### Co-Effects

The last type of side effect are the co-effects. Co-effects are the inputs, the data, that our application needs to make their decisions. 

For our bonus application, we basically needed 4 pieces of information: which clients have sign up for which bonus, the bets and deposit made by the client, and as the client has a limited amount of time to achieve the bonus, we also need to know what time it is.

As I hinted before, we kept all of our state in memory, and we were able to do this because the input source of the client events, was Kafka. If you are not familiar with Kafka, you can think about it like an immutable message queue, that remembers all the message go through it. 

So that when the bonus application started, it will ask Kafka for all the messages in the last couple of months, and recalculate the current state from all those events. Also, each event would be timestamped, so the application will use the event time as the current time in its logic. 

This is basically Event sourcing. At its heart, Event Sourcing and functional programming share a lot in common. 

![Event sourcing](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/033-event-sourcing.jpg "Event sourcing")

Event sourcing and functional programming go hand in hand.

### Benefits

So this is how the whole thing looks like if you put everything together

```java
public class KafkaConsumer {
    private AllDependencies allDependencies;
    private TheStateHolder theStateHolder;
    
    public void run() {
        while (!stop) {
            Bet bet = readNext();
            Effects effects = theStateHolder.event(bet);
            effects.run(allDependencies);
        }
    }
…
}
```

These are two dependencies that will be injected by your dependency injection framework of choice. 

The object with all the dependencies required to do effects, and the state of the application.

Here we are using the Kafka polling api, so the KafkaConsumer will be a Thread that will be reading new events from the Kafka topic.

Then ask our state to advance time, update the state and to return the effects that we need to execute.

And last we ask the effects to execute themselves.

By following this approach, some interesting things happen to our code:

![Benefits](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/034-benefits.jpg "Benefits")

First, our business objects had 0 getters or setters.

Also, our business logic was cleaner, because it will have no locks or synchronised methods, no try/catch blocks and no logging, because all of these will be done in a different part of the system. This removed a lot of noise from out business logic.

Also, there were no mocks in our unit tests, as both the inputs and outputs of our business logic where all plain values, so our unit tests were simpler. To test the side effects, all the impure parts of the codebase, we decide to use a small number of full-stack or integration tests.

And last, because we did not have to mock anything, we did not had any useless interfaces in the codebase. By useless interface, I mean those interfaces that just have one production implementation. 

### Functional core, Imperative shell

Now this style of design or architecture is called functional core, imperative shell. 

![Functional core, imperative shell](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/035-functional-core.jpg "Functional core, imperative shell")

The functional core is where all of our pure functions live, it is devoid of side-effects. The functional core is where we try to make as many decisions as possible, as it is easier to test and to change.

The imperative shell is where all the side effects live, all the ugly code about error handling, state and IO. We try to devoid the imperative shell from any conditionals, from any decisions.

And the aim is to try to make the functional core as big as possible, while we make the imperative shell as thin as possible.

As there are other architectures that have the same circular shape, I want to make something very clear.

![Not so funtional](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/036-not-functional.jpg "Not so functional")

If you have some code like this, where your core class depends on some interface also in your core package, and then you inject the actual implementation at runtime, this piece of code in ClientBonus is not functional. You functional core cannot dependent on any piece of code that could do a side-effect, not even on an indirect way.

Now I am not saying that you shouldn't do that, I am just pointing out that when you do this, all this code belongs to the imperative shell, so you do not have the benefits of your functional core.

# Dynamic (vs Static) typing 

Lets talk about the next big difference. Clojure is a dynamic language, while Java is a static one.

This is how a typical Clojure program looks like:
 
```java
clientBonus = Map.of(
        "client", Map.of("id", "123233"),
        "deposits",
        List.of(
                Map.of("amount", 3,
                            "type", "CASH"),
                Map.of("amount", 234,
                            "type", "CARD")));

((List) clientBonus.get("deposits"))
        .stream()
        .collect(
                Collectors.summarizingInt(
                        m -> (int) ((Map) m).get("amount")));
```

First, our domain objects are just a bunch of maps and list. And then our business logic consists on manipulating those maps and lists.

I am not sure what do you think about this, but for my Java sensibility, 

![alien](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/001-alien.jpg "Alien")

this is just hell, this code is the exact definition of unmaintainable code. If anybody in my team wrote that piece of code, I would demand for a very very good explanation of why are they doing it.

So in our bonus application, we decided to not do this at all, so we did not bring Clojure’s dynamic typing, and just use the Java type system.
                                                    
But when you are writing Clojure code, surprisingly, this dynamic typing is less of an issue, and I think it is because the Clojure core api is tailor to work with this dynamic data structures, so is a lot less hassle than with the Java API.
                                                    
But once you have write enough Clojure code, your mind starts to dysfunction, and then when you come back to Java, you start to have really weird thoughts. 

So when you type this class, 

```java
public class Bet {    
    private String id;   
    private int amount;
    private long timestamp;
}
```

you start to wonder, well, what is the value of creating a new class? What do I get out of this instead of using a plain map? 

And you notice that the first thing that you get is an almost useless toString method, you also get a broken equals and hashCode implementation. That is really annoying, but at least we have Lombok.

But what do you lose? Suddenly, you lose all the functionality that comes with maps, all of it, but what is worse, all the code that you have that works with maps, that understands maps, will not work with this new class. You have no code in the Java core API that can work, that can do anything, with this class. Except maybe for the reflection API. 

What is more, how many libraries are you going to find in Github that work with this new class? None.

It is at this point when you start to understand what Alan Perlis meant with 

> It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.
<cite>Alan Perlis</cite>

Each new class is a new data structure, that comes with zero functionality, that is completely isolated from any other code. This hampering the reusability of your code.

But what if we just leave our Bet objects as plain data? 

```clojure
{:type :bet
 :id "client1"
 :amount 23
 :timestamp 123312321323}
```

You have a sensible toString, the one that you see there. 

You also get a proper equals and hashCode for free.

But what is more important, you can still use all the core functionality that comes with your programming language, so you don’t start from scratch, you can reuse a lot of code. and you will find Github libraries that work with this code.

The Clojure community has embraced idea this idea of using plain data to represent as many things as possible. 

For example, you can use plain data to represent an http request, so what your http servers have to do is work with this map as an input

```clojure
{:request-method :get 
 :uri            "/foobaz"
 :query-params   {"somekey" "somevalue"}
 :headers        {"accept-encoding" "gzip, deflate" 
                  "connection" "close"}
 :body           nil
 :scheme         :http 
 :content-length 0
 :server-port    8080
 :server-name    "localhost"}
```
and just generate another map as an output. And you will do this by using the same core api. 

```clojure
{:status  200  
 :headers {"Content-Type" "text/html"} 
 :body    "Hello World"}
```

Think how much easier your test would become.

But also, you can represent other things as plain data.

Sql queries:

```clojure
{:select [:id :client :amount]
 :from   [:transactions]
 :where  [:= :client "a"]}
```

and the database result sets:

```clojure
[{:id 1 :client 32 :amount 3} 
 {:id 2 :client 87 :amount 7} 
 {:id 3 :client 32 :amount 4} 
 {:id 4 :client 40 :amount 6}]
```

HTML and CSS:
 
```clojure
[:html     
 [:body         
  [:p "Count: 4"]
  [:p "Total: 20"]]]
```

Configuration:

```clojure
{:web-server          {:listen 8080} 
 :db-config           {:host     "xxxx"                        
                       :user     "xxxx"                        
                       :password "xxxx"} 
 :http-defaults       {:connection-timeout 10000                      
                       :request-timeout    10000                       
                       :max-connections    2000} 
 :user-service        {:url "http://user-service"                       
                       :connection-timeout 1000}}
```

Even data about your data, your metadata:

```clojure
{:id        :string  
 :name      :string  
 :deposits  [{:id        :string               
              :amount    :int               
              :timestamp :long}]}
```

So by embracing this idea of using plain data, you end up using the same core API to write 

1. your business logic
1. your infrastructure code
1. your configuration
1. your metadata. 

Just one API that you need to learn and master.

So dynamic typing in Clojure is not as bad as you would expect, as it is brings a big chunk of benefits.

# Dynamic (vs Static) development

But types is just one of the dynamic vs static differences between Clojure and Java. Clojure provides a dynamic development experience. What does this mean? In Clojure, the first thing that you do when you have to develop a new feature is to start your application, and then what you do is just keep changing that running application, until you are done, without ever stopping it.

You do this by using a REPL.

Of course Java now has something call a REPL, but 

![Java vs Clojure REPL](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/037-repl.jpg "Java vs Clojure REPL")

just because they both have the same name, it doesn't mean that they are the same.

With a proper REPL, you never build or start your application, you grow the application from inside, one tiny bit at a time. 

A proper REPL gives you the same feeling, the same ergonomics, as a Unix Shell.

A proper REPL is like having a debugger all the time attached to you running JVM.

A proper REPL is the missing piece on your test-driven-development workflow.

[This talk](https://danlebrero.com/repl) is my best attempt to explain what a REPL is, but I think that a REPL is one of this very very alien things, that you really need to experience it, because it is very hard to understand or imagine.

A proper REPL is the thing that I miss the most when working with Java.

# Lisp (vs Fortan)

Ok, last section of the talk.

For our bonus project we obviously did not use the Clojure syntax, because if I did, I wouldn’t be here giving this talk. 

But for all of you that run away screaming whenever you see some Lisp, I have some good news for you.

The first thing is that, like other modern JVM languages, in Clojure you don’t have to type semicolons! This, I think we all agree, is a huge improvement over Java. 

In fact, this feature is so awesome, it is such a huge productivity boost, that Clojure goes even further, and in Clojure, commas are optional! Think about all those millions and millions of commas that you have typed.

![No more commas](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/039-no-more.jpg "No more commas")

Imagine if you could get all that time back, I would be at least 20 years younger.

But I know what you are thinking

![What about parenthesis](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/040-what-about-parenthesis.jpg "What about parenthesis")

what about all those parenthesis that Lisps is infamous for? Well, even here I have good news for you.

```java
.filter(removeCsvHeaders(firstHeader))
.map(splitCsvString())
.map(convertCsvToMap(csvHeaders))
.map(convertToJson(eventCreator))
```

```clojure
(filter not-header?)
(map parse-csv-line)
(map (partial zipmap headers))
(map ->event)
```

Those are two pieces of code from one of my teams. When we were learning Apache Spark, we happen to write basically the same application both in Clojure and Java. This is the main logic of the application, and as you can see they are the same, but there is an important difference. 

Let’s count parenthesis. 1, 2, 3 … The Java version has 16 parenthesis. And how many does the Clojure one has? 10. So the Clojure version has 40% less parenthesis.

But not only that, the Clojure version of the application, had one tenth of the code. 

![Java more parens](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/041-java-apps-more-parens.jpg "Java more parens")

One tenth, imagine if you could delete 90% of your code.

Ok, enough of silly jokes. Let's look at why Lisp people are so in love with their parenthesis. For this, I am very sorry, but I am going to have to show you some more Clojure code.

```java
List.of(
        new Symbol("defn"),
        new Symbol("plus-one"),
        List.of(
                new Symbol("a"),
                new Symbol("b")),
        Map.of(
                new Keyword("time"), List.of(new Symbol("System/currentTimeMillis")),
                new Keyword("result"), List.of(
                        new Symbol("+"),
                        new Symbol("a"),
                        new Symbol("b"),
                        new Long(1))));
```

This is a typical Clojure program. We are defining a function that takes two parameters and returns a map with the sum of those parameters, plus one.

Ok, maybe Clojure is a little bit less verbose than this, but this is essentially what you are doing when you write, when you type Clojure. What is this? Your code are just lists and maps, this is what we mean when we say that in Lisp, code is data, because look at this, it is actual data.

And because it is data, we can manipulate it, we can generate it, we can analyse it, with exactly the same tools, the same api, that we use for our Business logic, our infrastructure code, our configuration.

Metaprogramming, so writing programs that write programs, becomes a matter of working with lists and maps. This is very simple, yet extremely powerful.

This is why Lispers love their parenthesis so much. 

# Summary

So in summary ...

![Functional core, imperative shell](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/035-functional-core.jpg "Functional core, imperative shell")

Try to write as many pure functions as possible, they will make your application easier to understand and easier to change.

![Type balance](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/042-types-balance.jpg "Type balance")

After using Clojure, I see the dynamic vs static typing as a trade off. It is true that in Clojure I miss some of the refactoring capabilities that I get with a good Java IDE, and that I sometimes waste time chasing some misspelt word, but Clojure’s focus on data, somehow, makes the trade off a fair one.

![Development experience balance](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/043-development-balance.jpg "Development experience balance")

But after enjoying Clojure’s dynamic development experience, this is something I would never ever want to let go.

![Parens are scary](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/044-parens-scare.jpg "Parens are scary")

And please do not be scared about parenthesis. The same way you would not write Java without an IDE, you will not write Clojure without one. And the IDE is going to take care of all those scary parenthesis. And remember that there is a very good and powerful reason for them.

I want to finish with another quote by Alan Perlis: 

> A Language that doesn’t affect the way you think about programming, is not worth knowing.

Clojure has been, for me, one of those languages. Immutability by default, functional programming, dynamic typing and the repl, Lisp syntax and macros, everything as simple data.

All those things have been big lessons for me. They have change the way I approach problems, they have change the way I build applications, they have change the way I design systems.

But none of those has been Clojure’s most important lesson.

The key insight, the most profound lesson on my journey learning Clojure, has been that I have been how close minded I have always been to different ideas, just because of the fact that they were different to what I was used to.

If any of you had told me 5 or 6 years ago to learn a dynamic lisp, I would had say “no way, I am not going to waste my time”. And yet, here I am, preaching about Clojure. 

Clojure has open my mind to be curious about different ideas, even those that initially may seem disgusting.

So I want to encourage everybody to learn this year, or next year, a new language, and it doesn't need to be Clojure, 

![Languages](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/045-languages.jpg "Languages")

but pick something completely different to what you are used to, something that makes you uneasy, something completely alien.

I am sure that on that journey, you will learn something that you will want to bring to your day to day job.

And in the worst case, 

![Weird](https://danlebrero.com/images/blog/java-with-a-clojure-mindset/046-weird.jpg "Weird")

it will just make you weirder and harder to relate to.

Thanks a lot for you time.

But before you leave, please have a quick look at this video:

{% youtube MSFvEK8asY8 %}