r/java • u/political_one • Mar 08 '18
Do you use much functional programming in Java?
How do you use it or why do you not use it? I've used little bits here and there but it doesn't seem all that useful or pretty.
63
Upvotes
52
u/nuqjatlh Mar 08 '18
When it makes the code simpler and better, yes. It doesn't always. "Lambdas and streams everywhere" is simply wrong. "Almost everywhere" is right.
4
u/rally_call Mar 08 '18
Yes, I do it all the time. So much faster and more succinct for searching through lists and stuff.
Lambdas and streams everywhere!
5
u/Trailsey Mar 08 '18
My experience to date (anecdotal) is that it's used in a limited fashion, and most java devs are still coding imperatively.
I like it, and I think the reactive style has a lot to offer in many scenarios. I still think java land has a ways to go before we can get a full reactive stack (reactive jdbc for instance) but a lot of progress is being made rather quickly. I would expect this to become more normal over the next 2-5 years or so.
5
u/DevIceMan Mar 09 '18
I started doing FP in Java for a couple years. There's quite a lot that can be achieved which is far less verbose than the equivalent in non-FP Java. Unfortunately, I found that once you get past Lamdas and Streams, there's not much depth.
I started working on several FP libraries for Java, including some testing libraries and one which did something similar to Scala's match-case statements.
The problem I consistently faced was a lot of the stuff I wanted to do, and most of the FP libraries I was trying to create, were features natively supported in Scala and dome much better than I could hope to achieve in Java.
I currently work mostly in Scala, so I haven't really looked back much since.
I've taught a good number of people how to use a lot of Java's FP features. The people I have run into who say similar things to what you said, I've always given up on teaching fairly quickly. If you're looking for reasons to hate it, you'll find them.
Java FP is a tool; it's better or worse at accomplishing different tasks and it's easy to misuse. There wouldn't have been any point for the FP tools to be added to Java if they didn't have legitimate uses. Given Java's history, they're actually extremely biased towards the "don't add things" when it comes thing things like functional programming or advanced language features.
1
u/[deleted] Mar 09 '18 edited Mar 09 '18
I try to avoid shared (or, worse, global) mutable state as far as possible.
Local mutable state is fine.
I get very nervous when all of these are true:
staticfield, etc)Semantically
Thinking about the semantics of mutability, I believe that the ideal choice between immutability or mutability is often apparent by thinking about information flow: a mutable object can be seen as an object that can transmit information between all objects that share it -- it's an N-to-N communication channel.
Example:
I can now use aliasing to implement communication:
Problems arise when you "send" informations to recipients that you did not intend. In Java practise, there are two good ways to deal with that issue:
Edit:
Option 2. is easier to verify: you can check a class for whether it's immutable by "just" looking at the class itself (slightly harder for when there's inheritance). Option 1. is harder to verify since you have to look at all the code an object is passed to. I think that this is not a general principle in programming languages, it just happens to be so in Java.
1
u/wildjokers Mar 09 '18
I use them very rarely, quite honestly because I don't understand the JavaDoc for them. For example, I have no idea how the method reference shown here:
equates to type
I am not afraid to say I simply don't understand.
This article helped a lot, but I am still not there:
https://nofluffjuststuff.com/magazine/2016/09/time_to_really_learn_generics_a_java_8_perspective
I do use forEach quite a bit (especially when dealing with Map entries).
3
u/0x7374657665 Mar 09 '18
I'll take a string of
map,filter, andreducemethods applied to a collection over triple-nestedforloops any day.Most of the time developer time is more valuable than cpu time. In these situations I usually opt for a functional approach.
2
u/sudkcoce Mar 08 '18
Much less than I'd like to.
2
u/theelflives Mar 08 '18
I really like using lambdas, streams and filters for looking for objects in lists especially when lists of lists or lists of objects containing lists etc... are involved. I find it so much more readable and intuitive than loads of nested for loops.
I tend to shy away from writing to much actual code inside the lambdas though (I'd prefer to use a normal for loop than the stream version) as I've found that the resulting stacktraces of any errors that may occur inside the lambda to be difficult to read.
2
u/Maku270 Mar 09 '18
It is a bit of a mentality change to get comfortable using it, but it is very clean and quick when used correctly.
I did learn the hard way that streams do not play well with lazy loading.
2
u/volyihin Mar 09 '18
I always prefer functional style where it is possible. Sometimes it is hard to me starting with it, so i write in imperative way first, but than when solution was found it is possible to rewrite this method in functional style. And the result is always much cleaner.
3
u/GingerMess Mar 08 '18
There are situations where I've had to undo functional style due to abuse. Take for example this snippet I found in a method (I've reduced the code to only the relevant bits):
This is bad because any gains from the internalised foreach are arguably outweighed by the synchronisation on count, so while it may read nicely, there's not really any point. You may as well just do a standard foreach and increment.
On the whole though I use 'functional style' quite a lot, as it results in much nicer code to write and read.
2
u/willywuff Mar 08 '18
I use the fluent stream API quite often. Wen you got used to it, it is very readable in most cases. But you can easily do too much of it..
Other things are some (bin) functions as parameters to "inject" some behavior. But this is not very often.
2
u/not-just-yeti Mar 08 '18
It's much more painful in Java, than in languages that had support for higher-order functions built in from the get-to. [I find myself doing a lot of converting between streams and lists, in Java.] But at this point,
map,filter, andreduceare essentials for me, esp. with the->function notation.I think the biggest (related) best-practice is using a lot more immutable data, whenever plausible: it meshes well with the higher-order and stream operations, but it's a huge win in itself, reducing the interaction between distant parts of code.
2
u/FollowSteph Mar 08 '18
Yes, especially for lazy loading. Instead of setting the objects or overriding methods it’s a lot easy to pass a lambda and let the class that needs load and reload it as needed by just calling the load data lambdas. Saves a good amount of ceremony code and it’s easier to read.
1
u/evil_burrito Mar 08 '18
We're starting to as we switch to minimum Java8 for new work.
I'm a big fan. It reduces unnecessary verbosity, particularly with streams that replace for loops without losing clarity.
1
u/Jaystings Mar 09 '18
Not yet! I have an app that I'm working on that would benefit though. I'll look into the new Java features already lol
1
u/TotesMessenger Mar 09 '18
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/argv_minus_one Mar 09 '18
Yessssss… Let the object-functional flow through you…
1
u/Zoerak Mar 09 '18 edited Mar 09 '18
Just use it a lot (even overuse it for a while) to gain experience with it and you'll learn when to use it and why.
It tends to make the code more concise, composable and less stateful. Those are properties nice to aim for - regardless of paradigm actually.
1
u/boardhobo Mar 09 '18
I can't understand how Java didn't have lambdas and streams. I can't imagine doing what they do without them. Would suck pretty bad.
1
u/[deleted] Mar 09 '18
Writing FP in Java is like trying to build a water pipeline using water bottles and parcel shipping.
1
u/Benemon Mar 08 '18
Obligatory Vert.x plug. I’ve used this for high throughout IoT architectures. Works really nicely.
1
u/Tayacan Mar 08 '18
There are some scattered snippets in our code base at work. Nice enough to run into once in a while. :)
I've written a few of those snippets myself, too. The syntax for anonymous functions in Java is good. Coming from Haskell, it's kinda weird to see people making a big deal of basic things like map, filter and fold, but it's nice that they're there. :)
-2
u/[deleted] Mar 08 '18
FP is only every necessary if you do a lot of complex concurrent programming. In the enterprise world, that's seldom the case, especially since web frameworks typically handle thread management for you.
-16
u/StornZ Mar 08 '18
If you use the same code 3 or more times why not move it to a function or method? Then the code is only written once and you just have to call it when needed.
0
u/[deleted] Mar 08 '18
[deleted] — view deleted comment
0
u/[deleted] Mar 08 '18
[deleted] — view deleted comment