Pragmatic Functional Refactoring with Java 8

vJUG-similar-content-button-1

Speakers: Richard Warburton & Raoul-Gabriel Urma

You may be hearing a lot of buzz around functional programming. For example, Java 8 recently introduced new features (lambda expressions and method references) and APIs (Streams, Optional and CompletableFutures) inspired from functional ideas such as first-class functions, composition and immutability.

However, what does this mean for my existing codebase?

In this talk we show how you can refactor your traditional object-oriented Java to using FP features and APIs from Java 8 in a beneficial manner.

We will discuss:

– How to adapt to requirement changes using first-class functions
– How you can enhance code reusability using currying
– How you can make your code more robust by favouring immutability over mutability
– How you can design better APIs and reduce unintended null pointer exceptions using an optional data type

Speaker Bio: Richard Warburton

Richard is an empirical technologist and solver of deep-dive technical problems. Recently he has written a book on Java 8 Lambdas for O’Reilly. He’s worked as a developer in many areas including Statistical Analytics, Static Analysis, Compilers and Network Protocols. He is a leader in the London Java Community and runs Openjdk Hackdays. Richard is also a known conference speaker, having talked at JavaOne, Devoxx, JFokus, Devoxx UK, Geecon, JAX London and Codemotion. He has obtained a PhD in Computer Science from The University of Warwick.

Speaker Bio: Raoul-Gabriel Urma

Raoul-Gabriel Urma is a PhD candidate in Computer Science at the University of Cambridge. He is co-author of the book Java 8 in Action: lambdas, streams, and functional-style programming published by Manning. In addition, Raoul has written over 10 peer-reviewed articles, delivered over 20 technical talks at international conferences and is a professional Java trainer. He has worked for large companies such as Google, eBay, Oracle, and Goldman Sachs, as well as for several startup projects. Raoul is also a Fellow of the Royal Society of Arts.

IRC Chat

16:44 <ZoranR> do you guys know any good IRC client for mac os? or you are using the web page
16:49 <ZoranR_> got one – LimeChat if anybody is interested
16:49 <millrossjez> Hi ZoranR, I use LimeCHat
16:49 <millrossjez> sorry for delayed response, looks like you found it 🙂
16:50 <ZoranR_> thx. I just didn’t won’t to install a bunch of them to find one that works. This once seams nice
16:50 <Ishanp> Can we chat and video together in Kiwi IRC?
16:51 <millrossjez> Ishanp, I dunno, I prefer separate IRC client and a youtube window for the video
16:52 <ZoranR_> Me too. so i found one so I don’t need to switch tabs all the time
16:58 <theotown> Hey everyone 🙂
16:59 <theotown> We’ve got Richard and Raoul online, getting ready to start up in just a couple minutes.
16:59 <theotown> View everything here: http://virtualjug.com/
17:02 <Harmeet> hello to all
17:02 <Ishanp> when is it starting?
17:02 <Harmeet> yes, we are still waiting
17:02 <mpegjohn> Good evening all
17:02 <sjmaple> just about to start shortly
17:02 <sjmaple> sorry for delay 🙂
17:02 <procy0n_> at what is it starting??
17:03 <sjmaple> right about now…
17:04 <AllegroJazz> Hi there
17:04 <chairam> hi all
17:05 <ZoranR_> hi
17:05 <sjmaple> started now
17:05 <radcortez> lol
17:05 <radcortez> i just joined 😀
17:05 <sameer> getting error on video
17:06 <vadi> just refresh
17:06 <HarmeetSingh> same here
17:06 <ZoranR_> tđr
17:06 <HarmeetSingh> please refresh the page
17:06 <ZoranR_> refresh
17:06 <radcortez> and heard oliver mentioning me 😛
17:07 <theotown> Hey Roberto!
17:08 <HarmeetSingh> cool superman …
17:08 <marc_thats_me> “Let us know what is crap” Best quote of the day…..so far.
17:09 <radcortez> hey oliver 😛
17:10 <HarmeetSingh> really cool book
17:10 <HarmeetSingh> please launch Java8 in action in india
17:11 <sjmaple> marc_thats_me: please do – we use a different interface, so we’d love the feedback 🙂
17:11 <sjmaple> Hey radcortez !
17:11 <theotown> Hahaha, the longer your beard, the more functional you can be 🙂
17:12 <sjmaple> Please throw your questions my way and I’ll ask when I find the appropriate time!
17:12 <sjmaple> sounds like most people had to refresh the page to get the video to play, that sucks – did it work for anyone? 🙂
17:13 <iveivan> worked fine for me
17:13 <radcortez> it worked fine foe me
17:13 <marc_thats_me> I’ve been fine since the beginning
17:13 <HarmeetSingh> yes for me also
17:13 <lhermes> for me everything works fine
17:13 <sjmaple> Hmmm, I wonder if it’s a browser thing
17:13 <radcortez> safari here
17:13 <marc_thats_me> chrome
17:13 <lhermes> me too
17:13 <sjmaple> I bet it’s IE 😛
17:13 <HarmeetSingh> i have chrome
17:13 <nobbis> i had to refresh – firefox
17:13 <iveivan> chrome here
17:13 <HarmeetSingh> IE 😀
17:14 <ZoranR_> chrome here , and I had to refresh
17:14 <Trisha> I have chrome and it worked straight away
17:16 <mite_mitreski> hint : set video to at least 360p, it is very blurry on auto
17:17 <sjmaple> good hint, I’ll see if I can default that on the site, or mention it on start
17:21 <HarmeetSingh> i think, lambda expression use to override the functional interface method
17:22 <Dalius> sure I play
17:22 <lhermes> YEAH!
17:23 <radcortez> that horse would split at the middle 😛
17:23 <sjmaple> I’m not sure about gluing them together 🙂
17:23 <sjmaple> hahaha
17:23 <sjmaple> yeh, clearly a poor architecture
17:23 <radcortez> lol
17:23 <ZoranR_> lol
17:25 <HarmeetSingh> Functional interface have only one abstract method, so the compiler read the lambda expression as implementation of abstract method
17:28 <HarmeetSingh> is it true the reason of default introduce is need to implement Java 8 Streams?
17:29 <sjmaple> I’d say it was more to use Lambdas and streams with collections
17:29 <HarmeetSingh> s it true the reason of default methods, is need to implement Java 8 Streams?
17:29 <slb> HarmeetSingh: no it’s not true
17:30 <radcortez> well it was a solution to evolve the new API’s
17:30 <radcortez> without breaking compatibility
17:30 <radcortez> the collections API was very old and was lacking some improvements
17:31 <HarmeetSingh> hmm..
17:31 <tocsa_CCJUG_NJUG> Pipeline? I see fluid interface.
17:32 <HarmeetSingh> i think now the only difference between interface and abstract class is maintain object state ?
17:32 <jankrag_> And that is still quite a big difference 🙂
17:32 <Ishanp> can you please show addHeader meathod
17:32 <sjmaple> Yeh, as Richard said, it’s state and multiple “implements”
17:32 <slb> that, and the presence of a constructor in abstract classes
17:33 <sjmaple> yeh, good point slb
17:33 <HarmeetSingh> yes really
17:33 <sjmaple> can you have a default static init on an interface in J8? 🙂
17:34 <slb> sjmaple: either default or static, not both
17:34 <slb> I mean not both on the same method
17:34 <sjmaple> ok
17:37 <slb> sjmaple: ask why one would use currying instead of overloading
17:38 <tocsa_CCJUG_NJUG> Reminds me again the fluid interface: the interface’s function’s return value is the same interface type (an instance).
17:39 <carljokl> Isn’t currying combining functions
17:39 <wolfjb> sort of
17:39 <carljokl> Rather than overloading where new methods are created for each case
17:40 <carljokl> Albeit those can convert the arguments and call the other overloaded versions
17:40 <carljokl> Not really the same.
17:41 <wolfjb> it’s more like default args than combining functions… f(x, y, z) can become a partial function f(x, y) which has a default value for z that is applied to x and y when f(x, y) is called
17:43 <Ishanp> effective JAVA..
17:43 <slb> wolfjb: right, but if z=32 you can create g(x,y) that calls f(x, y, 32) without currying
17:43 <HarmeetSingh> yes
17:43 <wolfjb> slb: yep, haskell, ocaml, sml all do this for you behind the scenes, java 8 does not
17:44 <HarmeetSingh> what the java8 use behind the scenes?
17:45 <wolfjb> nothing, functional interfaces are exactly that, a regular java interface which you must instantiate
17:45 <HarmeetSingh> yes, this thing i know
17:45 <wolfjb> the -> operator is just syntactical sugar to do this for you in certain contexts
17:45 <HarmeetSingh> the compiler create normal method after compile the lambda expression
17:46 <wolfjb> there is no currying in java8, but you can get it’s effect by doing as @slb suggests
17:46 <HarmeetSingh> like bridge method in java generics
17:49 <sjmaple> JavaJim! Welcome:)
17:49 <JavaJimLondon> Hey Simon, thanks 🙂
17:49 <sjmaple> where in the world are you? 😉
17:50 <JavaJimLondon> Currently India, ducking power cuts at the moment
17:50 <sjmaple> 😀
17:50 <HarmeetSingh> haha.. power cuts, JavaJim you are in punjab ?
17:50 <sjmaple> oh, you just missed the disscussion on currying!
17:50 <JavaJimLondon> Mumbai
17:51 <HarmeetSingh> the powere problem is same as punjab in mumbai ?
17:52 <sjmaple> Should be JavaJimMumbai 🙂
17:53 <HarmeetSingh> the more if blocks are like dirty programming
17:54 <sjmaple> That’s one thing I like about Scala, the Option type
17:54 <HarmeetSingh> i think the groovy have some operator for this
17:54 <HarmeetSingh> ?
17:54 <sjmaple> makes a user calling a method/function think about what happens in the null case specifically
17:54 <slb> and you can always write: opt.orElseThrow(new NullPointerException()) is you are nostalgic 🙂
17:55 <sjmaple> +1
17:55 <wolfjb> lol
17:55 <slb> s/is/if
17:55 <HarmeetSingh> ahaha..
17:56 <sjmaple> you laugh, but someone somewhere is seriously typing that code 🙂
17:56 <slb> he’s not mentioning the “M” word, is he ?!?
17:56 <HarmeetSingh> sorry
17:56 <sjmaple> hehe
17:57 <wolfjb> sjmaple: I think I will do that later… 😉
17:57 <sjmaple> hehe
17:57 <sjmaple> it’s great Friday afternoon code
17:57 <HarmeetSingh> our java8 have two different flatMap
17:57 <HarmeetSingh> one in stream and other in optional
17:58 <sjmaple> reaching the end of the talk now… questions to Richard and Raoul please!
17:59 <ZoranR_> for me as a total functional noob, this session is great to get me up to speed.
18:00 <carljokl> I have kind of seen this presentation at the Skills Matter exchange but it doesn’t hurt to go over it again?
18:00 <Milan> Do you think the Groovy Elvis operator will make it to the next Java release? This Optional still feels a bit too verbose.
18:00 <HarmeetSingh> hey, what is the improvement in JVM in java 8 ?
18:00 <Maldivia> not completely functional related, but with JDK8, Map got a new default method: getOrDefault — is there a reason there isn’t a version that takes a Supplier, so for more advanced Value objects you’re don’t have the construction cost, unless it’s actually needed ?
18:03 <slb> using Optional seems to be *very* invasive
18:03 <slb> any suggestion on lifting existing functions or adapting classes ?
18:03 <Ishanp> difference between Map and FlatMap..?
18:04 <jankrag_> I would rather have my code invaded by Optional than by hidden NPE’s any day
18:04 <JavaJimLondon> Has Raoul grown a beard as big as Richard’s yet now he has finished his Java 8 book?
18:04 <millrossjez> nested null checks are pretty invasive too
18:04 <slb> but the good thing about NPE is that you have a stack trace, with Optional nothing will be done…
18:05 <slb> you really need to be *very* careful when you want to extract the value out of an Optional
18:05 <slb> e.g. you can’t use map() 🙂
18:06 <wolfjb> and in the case when None would represent an error… (getOrElseThrow scenario)
18:08 <HarmeetSingh> In optional for the value, we also need to check whether optional have a value or not, with isPresent() function in optional
18:09 <slb> raoul, you should have grown the beard like Mario Fusco !
18:09 <carljokl> I might be competitive with the beard these days
18:09 <HarmeetSingh> so, i think there are also nested if conditions ?
18:09 <wolfjb> yes, or use the getOrElse… methods
18:09 <Maldivia> Simon: guess it’s time for you to grow a beard again as well 🙂
18:10 <Ishanp> thankyou.. 🙂
18:10 <millrossjez> excellent session – thanks guys
18:10 <HarmeetSingh> thanks to all
18:11 <Milan> Thanks guys for a great session!
18:11 <lhermes> awesome session with a couple of interesting information
18:11 <radcortez> thanks guys!
18:11 <carljokl> It would be funny if the VJug overtakes the LJUG
18:11 <chairam> bye
18:11 <sjmaple> http://java8training.com
18:12 <HarmeetSingh> hey simon, i think there is little bug in chat
18:12 <sjmaple> You can catch Raoul and Richard’s training on that site
18:15 <sjmaple> thanks for attending everyone! 😀