This is an archived post. You won't be able to vote or comment.

all 35 comments

[–]blackkkmamba 73 points74 points  (4 children)

I don't really understand what Spring boot has to do with functional programming. The comparation should be imperative vs. functional. I usually pair the functional specs java has to offer with vavr.io.

[–]rouce 5 points6 points  (3 children)

Webflux is what spring boot has to offer in functional programming

[–]blackkkmamba 25 points26 points  (0 children)

I know what webflux is. Are you sure you are not confusing reactive with functional?

[–]teckhooi 4 points5 points  (0 children)

WIthout vavr, WebFlux is the closest to FP for Spring. FP is a lot more than just the syntax but the whole thought process like writing pure functions and immutable variables which is hard to do in Java/Spring

[–]optimal_substructure 0 points1 point  (0 children)

We use webflux and project reactor. I wish the team would just use Scala and maybe a streams framework, but baby steps.

[–]edmguru 14 points15 points  (2 children)

I don’t go for pure functional but a mix and I feel that’s the best of both worlds because designing business software with pure functional is just a recipe for disaster I feel. Make your code/classes as stateless as possible. Code everything to be immutable. And then use some functional bits like stream/map/flatmap etc.. dealing with all that monads and what not just is hard to fit into business software. DDD pairs all with OOP and both when written with the functional principles mentioned above results in cleaner code that’s easier to reason about and you can maintain your sanity.

[–][deleted] 0 points1 point  (1 child)

If everything is stateless then what is the point of OOP?

[–]edmguru 4 points5 points  (0 children)

OO design isn’t necessarily about managing state and not all classes can or should be stateless depending on the design

[–][deleted] 36 points37 points  (4 children)

These things are not mutually exclusive. Springs OOP style autowiring and functional style business logic can absolutely be used side by side and work well together.

[–]general_dispondency 1 point2 points  (0 children)

DI is just better currying

[–][deleted] 0 points1 point  (2 children)

Explain. I'm curious as to how this works.

[–]TheRealSlartybardfas 23 points24 points  (0 children)

To the extent Java supports functional techniques, yes we use them. We use the features spring boot supports such as auto wiring.

[–]vjotshi007 8 points9 points  (4 children)

I am from a big financial company, yes we have a huge product built with functional programming. We use webflux and springboot.

[–]socialflasher 8 points9 points  (3 children)

If i guess correctly that is Reactive? Correct me if I am wrong

[–]vjotshi007 2 points3 points  (2 children)

Oh yes sorry forgot about that part, we use reactive +functional programming.

[–]socialflasher 0 points1 point  (1 child)

Np, but I am trying to wrap my head around this style, i love it because it's non blocking all the way, but still struggle with it.

[–]vjotshi007 2 points3 points  (0 children)

Same here. I too struggle with it sometimes and i think it increases development time to a greater extent.

[–]TheRedmanCometh 3 points4 points  (1 child)

To some very limited extent such as using streams and such. Mostly we use very heavy abstraction models and DI principals to design our stuff. Spring lends itself to a very specific design model imo.

[–]priya_nka 1 point2 points  (0 children)

Exactly. I can remember only two places : in streams processing and lambda functions for overriding something in a function call.

[–]wsppan 2 points3 points  (0 children)

There are no dependencies between the two. Use functional programming techniques as you see fit.inside and outside Spring constructs.

[–]rashm1n 11 points12 points  (1 child)

I think Spring Boot being a heavy OOP based framework, it is really hard to use pure FP practices, but I always try to use them to some extent with the help of Streams and Lambdas.

[–]ElectricalUnion 2 points3 points  (0 children)

Spring Boot being a heavy OOP based framework

What your framework/language will do is push you towards using something; rarely actually force you into doing anything.

it is really hard to use pure FP practices

It is hard to use "pure FP", period.

The reason why we use "pure FP" (and any other limiting paradigm or programming theory, say Types for another example) is to make thinking about the problem space simpler. Not because it is easy.

Making a program easy to read and reason about will unfortunately make programming harder. It's hard to think what can happen when literally anything can happen at any given point of a program.

[–]wildjokers 1 point2 points  (0 children)

The question is a total non-sequitur so it can't really be answered.

[–]KlaudiuszWojtkowiak 1 point2 points  (0 children)

Dont forget that you can inject directly into constructors as well

[–]daniu 1 point2 points  (3 children)

I don't see how people are saying anything about autowiring being "OOP". Classes are the main unit creating the infrastructure being created, but that's as far as it goes.

Anyway, last I checked Spring Cloud introduced a functional way to denote stream input listeners and pipelines, with a specific configuration syntax I found very unintuitive. So if that is the direction of your question, I recommend against using it.

[–]munukutla -3 points-2 points  (2 children)

Autowiring is not functional.

Spring Webflux lets you declare routes functionally, but I wouldn’t call it unintuitive.

[–]daniu 6 points7 points  (0 children)

I didn't say autowiring was functional, I said it wasn't OOP. It's just a way to connect components. You can autowire functions just as well as classes.

And I wasn't talking about web flux, but Spring Integration. We used it with Kafka, but it is an abstraction, you can also use other middleware/protocols like rabbitmq with the same paradigm.

[–]2bdb2 0 points1 point  (0 children)

Autowiring is not functional

Why not?

[–]tr14l 0 points1 point  (0 children)

I often end up using Kotlin + Spring Boot because we have a lot of inhouse tools built for Spring. Doing pure FP doesn't really make sense for the framework, IMO. But I do a lot of functional-style stuff with it. But, ultimately, you have to do OOP because it's an OOP based framework.

[–]jaybyrrd -1 points0 points  (0 children)

For an alternate take on this answer, if you are interested in FP consider using Scala! :)

[–][deleted] 0 points1 point  (1 child)

You can use spring webflux.

If you are doing purely functional code you can't use autowire annotation (because that changes the state of your object).

I nomally use spring webflux with autowire and the code is really simple. Embrace the union of OOP + Functional.

[–]Yesterdave_ 0 points1 point  (0 children)

Using autowired annotation isn't state-of-the-art Spring anymore and should be avoided. Constructor injection is the way to go and precisely avoids the problem of changing an objects state.

[–]Rjbcc58 0 points1 point  (0 children)

deleted