Going functional with Java?

For the last 3 months, I started to work with a completely new paradigm for me – programming. And with the new programming language – , which is not so new for me, because I know and Scala runs on JVM. But the way of writing code in Scala is completely new. I decided to put a bit of my thought about going into the direction into this post.

For a long time, the only one way of writing code for me was OOP and so-called imperative style. I have read a bunch of books about clean code and architecture and patterns. All these books mostly based on OOP principles and provide examples with some OOP language, as Java.

Despite the fact, that still most of the time I work with PHP, I really love Java and always had thoughts to switch to Java world completely. I even familiar with Spring framework and participated in development. But, I stopped actively learning and work Java on version 7, before new features, such as stream and lambdas were introduced.

And 3 months ago I got a new job. And at this job, some system for crawling web pages were written in Scala. Unexpectedly for me, I said hello to functional programming.

My experience and the path

So, I started from watching video courses and reading articles. The main recommended way to introduce yourself to functional programming, especially for Java developers is:

functional learning curve

functional learning curve

  • – start using functional features in Java as much as possible (lambdas, stream, map, Optional, filter, etc.)
  • – import into your project libraries like functionaljava.org, etc, which add more functional features and use them
  • – start writing code in Scala, still use OOP in big, but use functional way in small, in implementations
  • – during a time start to switch to more functional
  • – ???
  • – you are Scala developer now

My experience was a bit different :). I hadn’t any time for step 1, so I started from the step 2 and very soon started to dive deeper into functional features. Ощутимый switch for me became real after I learned how to avoid null with Option, Some, None and use of map without промежуточный variables.
Actually, I tried to work with Java stream features before, so I knew that they exist. But I haven’t understood their power. But today, after trying to run some Java code example which I have found on the internet, I got typical NullpointerException. And my natural reaction to that was rewriting a method which could return element of the collection or null – to the functional way with the use of java.util.Optional<>  class. That’s why I wiring this post.

Let’s go through the example.

It is a simple example, which emulates work of some shop. It contains objects like Employee, Department, Visitor, and Goods. First, shop initialized with departments and employees and goods. Then some visitor comes, and Administrator search for the free Consultant, to assign him to the Visitor.

Do you see the problem with this code? What if we will call the search for free consultant one more time, and there is no more free consultant? Let’s emulate this situation

Boom, NullPointerException occurred.

What if we have this program running in the process, and we want it to keep running even in such fail situation.

So, in the imperial style, we need to check first if we got some Consultant. If (consultant !== null)  blah blah blah. But can we do it better? Let’s look into the getFreeConsultantOld() method first (how it was originally in the example, not my code!)

And it’s new implementation in more functional style

It is shorter and as for me more readable. But this is not the main benefit. Now instead of returning null we returning Optional, which is something or nothing. And this is the normal situation, not an exception anymore. Let’s adjust client code to the new method.

Looks better? If we will put visitors in the List and will try to find Consultant for them in a loop – it will not fail. Just some visitors wouldn’t be consulted. And if we will call the same code second time, the same, no more exception.
But this code still looks too imperative, let’s process vipVisitor with the better functional approach.

Now it is functional. Maybe could be better. In Scala, I would also improve code inside the map and will replace the switch with the pattern matching. I don’t know if this possible in Java and can’t check because I don’t have the Internet access right now. Yes, I’m writing this post in the LibreOffice Writer, just because I’m bored.

But the most important part of this small Java code refactoring into functional way is, that I have never worked in such way in Java. So, how I did it, without the Internet and SO? I just figured it out in 10 minutes from my Scala experience! And I would not do it without such experience. It will even not pop up in my head, I would consider an example below normal and would just handle an exception in the usual imperative way. Just 3 months working with Scala directly changed my mind. But I know, this is only the beginning…

My advice here, from my experience, if you want to bring some functional programming into your code, don’t do it smoothly, as recommended. Instead, pick up some language with functional support, maybe not so functional like Haskell and Erlang, but still – functional. And learn it first. Then, it will be much easier for you to apply this way of writing code in the language which you use daily. Otherwise, you will not see out of your box and you will not recognize new patterns.

For me, it’s a pity, that PHP has so limited functional support. And JS, with which I work a lot also, even with ES6, still more async oriented language, then functional. But much more functional, then PHP, still. It is possible to create chains of the map and reduce methods and use short functions syntax in JS, but not in PHP. That’s sad. Anyway, at my current work at least 20-30% of all programming I could do in Scala. :)

And here in my blog, the first (and I hope not the last, because I post very rarely, especially when I have the Internet) article about Scala.