Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Does syntax for functional programming get difficult to write in case of complex business logic?

 
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case of Java functional programming it is simple to write the lambda for e.g functional interface to add 2 numbers etc. In case the functional interface has complex business logic (much more complex than adding two numbers), does the Lambda syntax using arrow not become difficult to write in terms of arrow syntax?

Thanks
 
Rancher
Posts: 3371
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can, yes.  Usually in this case, the solution would be to refactor that complex code into a new method (or set of methods, or class, or set of classes) so that the lambda itself becomes simple looking, like "x -> complexMethodCall(x)" which probably becomes a method reference MyClass::complexMethodCall.  If you have a lot of method parameters, then you may create a new class to encapsulate the list of params into a single object.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. If we have to keep that complex business logic in a method complexMethodCall , then what is the benefit of using the lambda function in this case as compared to the case in which we don't use any lambda and have only the method complexMethodCall.?
 
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The benefit is that it addresses the issue that you asked about in your original post.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we have that complex business logic expressed using just the lambda then we get the advantage of lambda. However if we are using another method then what is the lambda doing when everything is to be taken care by that method.
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lambda is being called by the unspecified "functional programming" which you asked about in your original post.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But is the sole purpose itself of using lambda functional programming not lost here?  
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know. Why don't you show us an actual example? Tell us the purpose of using a lambda in the example and explain why you think it's lost.
 
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:In case the functional interface has complex business logic


By definition, an interface doesn't contain any kind of logic, simple or otherwise. All an interface does is define an API. I think everyone who has answered so far has some kind of interpretation of your statement but it's very important to be explicitly clear about what you're saying/asking, otherwise it's likely you'll get a mixed bag of answers depending on whatever assumptions and interpretations people based their answers on.

... does the Lambda syntax using arrow not become difficult to write in terms of arrow syntax


Here, I imagine you're asking if it's not difficult to write something like this:

Now, if the "lots of logic" part of that snippet consists of many lines of code, then yes, it might be hard to write it and test it but actually using it is no harder than doing this:

So again, what are you asking about: using or writing a lambda with complex logic?

Just a note: if "lots of logic" consists of more than 10 lines of code, I would seriously rethink the decision to use a lambda expression. At the very least, I would extract all that logic to a private method so that I can just write:

But then if you find yourself doing that anyway, you have to think "Why not call the myPrivateMethod() directly instead of using a lambda expression?" Of course, there might be a valid reason to do so such as implementing the Strategy design pattern using lambda expressions.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Yes the last paragraph of your answer that exactly was my question that the sole purpose is lost (as one could have directly called that method itself)
 
Junilu Lacar
Marshal
Posts: 14046
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks. Yes the last paragraph of your answer that exactly was my question that the sole purpose is lost (as one could have directly called that method itself)


I don't know what you think is the "sole purpose" of lambdas. There are many things lambda expressions allow you to do in Java so I can't relate to the idea that they have a "sole purpose."

At the risk of prolonging this discussion even further, I still don't feel like we're on the same page, that there's a fundamental misalignment in understanding here. I don't see how you can reason that putting complex logic in a method that you call directly diminishes the ability to do functional-style programming which, unless I'm totally misunderstanding you, seems to be what you're saying. Without lambdas, implementing aspects of FP like higher order functions would be difficult and awkward at best.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. If have got the answer from my question when you said said strategy design pattern may be one of the reasons.

Are lambdas used more when implementation is simple and for complex ones it is sometimes avoided?
 
Paul Clapham
Sheriff
Posts: 24654
58
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Are lambdas used more when implementation is simple and for complex ones it is sometimes avoided?



I can't imagine why. Sometimes you have to do complex things, but I don't see why lambda expressions would be a problem there any more than (say) for-loops would.

It's true that people might avoid using lambda expressions because they don't understand them, but that's a separate issue. Of course people are likely to avoid language features which they don't understand, especially when they aren't motivated to learn new things.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because why not call that method directly(the method in which we have extracted that complex logic)  instead of using a lambda expression?"  any disadvantage of doing so?
 
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Calling methods in an imperative style may require a loop and lots of boilerplate code which can obscure the intent of the repetition.
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why. Can't we just have it as a static method and directly call and that's all?
 
Saloon Keeper
Posts: 10669
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are confusing two unrelated concepts: When is a function called and how is a function defined.

You may not always want to call a method directly. For instance, the actual call site of the method may not know WHAT method it has to call. So you either pass the call site a method handle or lambda expression.

Whether you pass a lambda expression or method handle may indeed depend on how complex the code is. If the code is a one-liner with a clear purpose, you may pass it as a lambda expression. When the code spans multiple statements, you may want to make a separate method out of it, and then pass a method handle.
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What difference will that make?
 
Monica Shiralkar
Ranch Hand
Posts: 1257
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java in case of complex logic, inside the Lamba expression we can put the complex logic and after that we can EXPLICITLY place a return statement.  I am not sure whether it is possible in Scala.
 
Campbell Ritchie
Marshal
Posts: 65806
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:In Java in case of complex logic, inside the Lamba expression we can put the complex logic and after that we can EXPLICITLY place a return statement. . . .

That doesn't make any difference to the difficulty of writing the code.
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!