Java 8 kicked ass. Why? Because it not only introduced new tools to play with, but also a new way of thinking and programming.
Functional programming
Well, to be exact, this new manner of doing things, functional programming, obviously isn’t new in the programming industry. But in Java language, until Java 8, we were restricted to being an ‘imperative boss’ with our computer. We interacted with him in a not-that-smart fashion, having to do a lot of work to translate our business intentions into the code we had to write: as imperative bosses we were forced at all times to tell the computer not WHAT we wanted to do but also HOW we wanted to do it. We had to make the effort to use low-level constructs in order to express our thoughts: fors, ifs, etc.
It was like, instead of just saying: ‘tell me, computer, do we still have clean spoons left?’, we had to say: ‘hey, computer, use your right hand to open this drawer using all of your fingers, search into the spoon compartment having this coordinates, get all the spoons with your thumb and index finger, etc, etc’. You get the point: too much detail to worry about…
Risky business or risky logic?
This is not only tedious but risky: telling the computer about this low-level stuff, it was easy to make mistakes while worrying about the how instead of the what: who hasn’t got into an indexoutofboundsexception just because we forgot to especify a -1 somewhere in the for loop declaration?
Even worse, with that imperative mindset you could get lost inside the how-to and forget the most important thing: the business logic determined by our business requirements. Who hasn’t finished an algorithm to retrieve something and then said to himself: why was I doing this; what did I want to do in the first place?
Besides, before Java 8, after having finished with telling the computer about the HOW-TOs, we usually ended up with a mess of code that you (if you really are a PROgrammer, that is, you also care for your human readers), you had to wrap up the low-level stuff inside descriptive methods/classes, etc., in order to transform the low-level stuff into something meaningful, digestible by a human reader. This is, you had to make an effort to make clear the WHATs.
Java 8 expresso
Now (well, since 2014), Java 8 has introduced the functional mindset: almost all of the day-to-day how-to operations that we need to do are wrapped inside intelligent APIs that will do that low-level stuff for us. So a lot of the risk of making loop’s index mistakes, dangerous mutation of the state of our objects inside loops, maintaining the state of unnecessary auxiliary variables and a lot of the final cleaning of our code in order to make our intentions clear, a lot of this job will be done for our sake, thus, reducing not only our effort but our program’s complexity. And, the most important thing: allowing us to focus our time in the business logic.
This comes with a price, of course: the price of getting familiar with new constructs and APIs like Streams, lambdas, functional interfaces, etc. Believe me, it’s not only worth it but also simple.
You might find interesting that when my attraction to Java 8 was born in 2018 (better late than never) I was so shocked by it that in a few months I ended changing jobs because I was stucked with Java 7 in my job at the time. Once I have tasted the power of Java 8, there was no way back.
At the beginning, like everything new, it will seem perhaps a bit confusing if you haven’t used functional tools before (like lambdas and such). Don’t worry: as with everything you can make it look very complex, like when you say ‘dependency injection’ or ‘cyclomatic complexity’. But believe me: in the end you’ll not only get it as I did: you’ll also see that the complexity was in the old way of doing things, before Java 8.
So, remember, behind Java 8 the greatest thing of all is the change of paradigm or mindset: from an imperative one, focused in telling the computer HOW to do the thing you want to do, to a declarative one, in which you just have to tell the computer WHAT you want to do.
In future posts we’ll be uncovering the simplicity behind all this functional stuff. You’ll see yourself using (and enjoying) it in no time!
If you want to have a taste of the Java 8 delicatessen coffee, take a look at the wonderful book Functional Programming in Java, by Venkat Subramaniam. Being fun, intelligent, relatively short and to the point, it’s the best resource I know of to migrate your mindset.