• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another question to Jeanne & Scott

 
Kranti Sri
Ranch Hand
Posts: 35
Chrome Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne & Scott,

I am little aware of the 'new' features that Java8 introduced. I am kind of intrigued by the functional programming aspect of it. How do I change my way of 'java' coding (from java 6 to jave 8). eg: the way we can write lambdas etc,

Thanks again,
K
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kranti,
The big difference with functional programming is that you think about what you want to accomplish rather than how to do it. For example, suppose you needed to tell someone else to print a list of all the odd numbers in a list greater than 15 without any duplicates. Not how to do it. Just what you need done. You might use words like "unique", "distinct" or "filter."

The code looks similar:


What probably isn't in your description is words like "loop" and "set". Here's the non-functional code:



To me, that's the biggest difference, that you aren't thinking about implementation as much. Also, keep in mind there are new "idioms" to learn. Java programmers can write for(int i=0; i< nums.length; i++) without even thinking about it. That comes over time with lambdas. I wrote a lot of lambdas while doing Groovy scripting. I saw myself switching from thinking in Java to thinking in functional programming. You can see some of the code from that journey in my blog post about thinking in functional programming.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find yourself writing a lot of small classes, especially anonymous inner classes or event handlers, you are going to love lambdas. It basically lets you define methods without having to define a class, and pass them around as such. The amount of time you saving while writing code is definitely noticeable once you get used to the new syntax.

One thing I'm not a huge fan of is default/static method implementations in interfaces. While I understand "why" they were added, aka default methods allow backwards compatible of old classes with newer versions of an interface, I'm very concerned inexperienced Java developers may use them in new interfaces, in some sense blurring the line between abstract classes and interfaces. I agree with the concept of them, but worry about the kind of code they might lead to.
 
Kranti Sri
Ranch Hand
Posts: 35
Chrome Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like people who were used writing code in a non-OO language (like VB6), had to make a big mindset switch (think in objects working together instead of a bunch of procedures/methods invoking each other), you'll need a mindset switch as well when you are using lambdas and you need to think more in terms of what you want instead of how to do it. And that could be hard in the beginning.
I remember in my fnal year at college we could follow an extra course. In this course we had to learn a functional programming language (Clean) and make exercises. The only thing I remember, in the beginning it was hard to think in nothing but functons. But once you were used to this different way of approaching a problem, it was business as usual and you could develop just like you were used to.

With lambdas you'll be able to focus much more on business logic and get rid of some boiler plate code. For example no more helper methods (with always the same loops but different output) to retrieve the name or salary of a list of persons, you simply focus on the (in this example, very easy) business logic and write something like persons.stream().map(Person::getName);.
I have to admit I didn't have used lambdas until now, but I have seen plenty of them in the past few years. And one thing I always wonder about: how to debug these lambdas and predicates, this functional code. If you look at the code snippets from Jeanne's post and you have a bug in the non-functional code, you simply put a breakpoint on e.g. the if-statement and you can debug and see what's happening. But how do you achieve the same with the functional code snippet? It seems a lot harder to do! And you could argue that you only have to test the predicates. And that's indeed true! But what if you really want to loop through the list and see what happens with each value?
Oh yeah, and another thingy about lambdas: you have almost infinite possibilities to solve a particular problem. Jeanne's functional code could be written asSo all these different approaches will result in more debate about which ones is the best solution for the job.

Kind regards,
Roel

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic