Functional Programming Patterns with Java8 by Victor Rentea

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone hmm are in for some fun let's have some fun shall we okay a bit about me first I'm Victor I speak Java although of course with the years I had to learn other languages as well back at work they call me a lead architect at IBM but please don't believe I'm sitting there in the corner of his drawing Visio diagrams on the world that's not me I'm actually leading the most difficult project challenging projects we have and basically that's me helping everyone on the floor or maybe in the entire building to solve any kind of problem related to job and get to see indical understand I get to see all sort of really nasty issues and I love that I'm one of those guys those strange guys who still use those lost extreme programming practices like Perl programming aggressive continuous refactoring and test-driven development I'm a weird guy back home now I still in the community side let's say I'm one of the key contributors in the local Java user group back home in Bucharest and I also started recently the wicked software craftsmanship community aimed to inject passion in developers and help build a technical culture in the company because in my opinion this is the most important goal if you manage to empower to to help to promote to to gain your developers on your side then your company will be good your company will just be fine now I like clean codes I'm really into this fling code stuff and I've talked about linkage in so many conferences and cities and countries all over the world and everywhere I went I found the same passion enthusiasm about how to write well-crafted code code that is easy to understand easy to maintain a pleasure to work with you can find the solution of my talks here on my website but just tell me one thing how many of you have more than eight years of experience raise your hand more than eight years oh I love you guys although your guys should be training teaching coaching the younger the junior the Kumbha comes to our profession it is our moral duty as a brother as a professional to help the younger grow and I have done just that five years ago I've started of course with internal trainings and over the fast over the past five years I've trained more than 1,000 developers in more than 100 training days on challenging companies initially of course I start with simple topics and our frameworks libraries basic stuff but then I moved on to more advanced agnostic topics I teach design patters in my local university for the fast for the past four years and Kling coordinate testing I'm also into Java performance in Scala but you see when I come to a company besides just teaching what what I'm supposed to be teaching I'm I also try to build this culture that I've mentioned this technical culture this to inject passion to developers because this is the ultimate if you reach that no you will have no more problems really so if you want to see things if you want to hear my thoughts if you want to see you nice metaphors quality stuff do follow me on LinkedIn on Twitter but enough about me let's do dishes laptop conference right so we should turn on this which yes of course we should turn on the hot countries and we should code hint you can lower the play rate in YouTube afterwards right you know that good so just to warm up a simple stuff I would like to get this list of users to the into the to the interface so for that purpose I will build the user fade which has this user repository dependency then I have public list of user details I'm returning this to the user so there is an interface yet all users I will first get the users from the from the repository then I will create the list of details then I will iterate over all the entities in my list I will create a detail for each of them and I will just set user name to be the same as the user name enter in the entity the full name is first name plus last name uppercase of course and then it's active in case it was not yet the activation equals null and add to a list and I will return the reason or not but this is not the thing this is not something I very proud of they standard code that I will write over and over again enum in so many situations and every time I will write this stuff over and over again this stuff the only thing that matters is this particular lines these particular lines is what really matter so what I will do I will just extract them as to details to DTO the eto and then our same merciless savages return of course this is warm-up right stream and then map and then this four dots too detailed not no issues about that collect the list not that one the list thank you and there we go this is the one line that is you should write instead note however that I don't want to in line this funny I don't want to have this huge lambda here I could type this but this is very very very ugly right so whenever you have a big lambda that start with this do extract the function like I just did here but in certain situations like this for example this mapping is so easy that I can even push inside the constructor of the DTO this is a bit weird but details are allowed to depend on entities not the other way around so look at what I will do I will just okay and then do this and there you go yes we also have a sort of a I love LJ as you want so this is it by the way I'm using eclipse sorry okay and then this will result to the user DTO four dots new very nicely and I don't know I no longer need this however if this mapping grows too big or if at some point requires something like let's say Auto wired perhaps some private other dependency right if it needs another component to do it stuff right to call some method on that dependency this will never work no matter how much the DVD guys would want to inject components inside in the table this voice won't ever work so I will just remove that I will come back to the previous solution very nicely here it is come on control-z do you know work what controls he comes from Zoro it takes a lot of strength emotional strength to just revert your work and try again but this is something you need to do at times you need to be prepared but again this is growing too big and I want it out of my eyes that that's why I will obviously create a class user and mapper in which I will just move this method after I make it public to be able to call it okay I will just fake that I put here a component this won't ever run in this speed you can imagine by the point is I will then be able to auto wire the private user mapper mapper mapper mapper Thank You mapper and then same Apple for dots this video this will work with just fine so I've just pulled this this mapping out in an instance object this other instance subject managed by I don't spring for example would be able to use other dependencies in turn to do it stuff okay so ways to hide those logic that that bit of logic and put it in a method inside the same class in another in any item class in a static method in another instance class in another as a method as an instance method of another class and so on but this was just warm up right and maybe at times you may get warmed up you might even write such things oh yeah this guy implemented one use case with one single chain of streams this guy should be hanged for Christ's sake so the worst thing you could do after you collect is to stream again this is evil this is despicable you should never do that right so for a minimum but I want refactor this I will just point out that this should be replaced with a methods with the helper let's say synthetic yes I like to call them which is will be named even not deleted in order to be able to use the for dot notation very nicely about that but again it's the boring to refactor this and pan is not too long today so I would like instead to focus on the most frequent bug in the world the null pointer exception which was it or I mean no wasn't all is there right let's talk about the billion dollar mistake of search our hunt and Richard who invented noon okay so let yeah they dive into it I want to be the discount service which will return a string get discount line for a customer this will return the discount percentage to be get discount percentage of this customer that member card then in the private integer get discount percentage of member card if I got fidelity points more than 100 for example I will return five percent reduction if the fidelity points are more than 50 our return three percent reduction otherwise nada nothing new and I want to test all this for 64 tenants for no member card nice so the first thing I want to do to test this method is to instantiate this new D discount service of course which will be something like this and then I will cease out and I will say service dot get discount line of a new customer with a new member card of 60 right 60 points and I will run this as a class and I will seek 3y 3 because I fell in between those and actually I got here so this is a 3 I see right there nice this is fine but what will happen in case I only have 10% I mean 10 fidelity points let me see what happens oh it is just display them all to your user hell congratulations his trusting you will fail will decrease right so this cont percentage should and no it should not be printed and you panic of course it what can I do and what do you do you look where does this noodle come from it is this concatenation this null here gave us problems oh then what can I do you all know right this is the and if this is not no we all done arrived we've all done that many times many many times and otherwise we turn the beloved and effect there we go this should fail you should fix it but then you walk your operation you hear some people there in a corner saying that where each time will give you trouble you should consider an optional and you are curious about this option and you jump on it and you say ok I use this option what is this officer it is a box with may contains I think or not well well this problem really really comes from is that this function returns a mole that is unexpected by who calls that function so instead of just shamelessly determining unknown I will return an optional of integers this will be an optional empty and this will be an optional of three this will be an optional of five and then this will be will give me back an optional of integers so what I do here is very easy right d is present this is present III concatenate right that's it it's easy oh wait a fill of three why because you see D is now an optional of integer so I we need to do it yet good but then this isn't very different from the previous solution right I mean we still have that if there so that with optional whenever you use optional you should start with the optional what I mean by that is do B and then map and then in case there is a discount inside I want to prepend it with this stuff like this very nicely return and then in case there was nothing in the box or else entering and there we go no more it's very nice thing cleanly I get this up it should clash here right this is the optional let's say but it will have a short short short life because as soon as that I was in I will in line it of course I will say map and then orders very nicely and I got rid of the problem without using any ifs which is good then then you start working on the third use case in case a customer doesn't have any member can not forever nothing I mean if you run this you will see Sir Charles Anton Richard here with us today no point of exception and whenever you see a nullpointerexception the first impression I see in people is panic oh my god oh my god Oh shame on me I have a bug don't do that don't ever do that always use the bugs in the spec traces you get as a way to improve your skills because in this case the first thing when normal developer does is oh my god click here where is coming from here okay so rapidly rapidly if if God is null oh my god let's watch the shame away return empty but this will just add one more responsibility to this the same pile of responsibilities you shouldn't you shouldn't do that instead you should all again think about the optional so now this comes the question where this these newer comes came from actually it is this place right this Gator got me unknown which I then crashed here with more pointer exception so instead of basically the null surprised me when it came from here so look what I will do I will have the courage to touch on holy things like jetters in the end it is all my god what is he doing how does he dare to mess with the auto-generated getters and setters oh my god can we have logic an together said yes you can so then the point is here it this will give me an optional now what I will do next is use the IDE driven development do you know how I did riff on development works you press in the in your of course in the DJ art enter enter I will press this there you go I have an optional inside right yeah so what right do we do here is very easy right if it's not present I will return empty but again this didn't save me anything I still have that if so again whenever this happens we should start with the optional how did we do just now we started with the optional and in case there were something inside we prepended something so again we should start with this which is now an optional oh my god it's an optional so we should start with this let's do this and in case there is a card so map in case there is a card I will get this country centage out of now but I did a mistake I did a mistake I forget to do what I forgot to do Zoro again now really think of it afterwards how much pain it takes some sometimes to change the thing that you create very very very very painful of times so really what will end up here it's something I think like that right I will start from customer yet not suffer that good now this will ensure no an old card will enter here so I can get rid of this one very nicely and now if I run everything looks fine right almost because you see what I did when I if I if I do this a second and this D is now an optional of integer again it happened before but the problem comes that this map will return me you know when you buy up your studio your your key the present and you wrap it in two wrappings right for Christmas between two twenty two to increase the thrill of discovery this just happened to us without talking about monads without talking about anything nasty that's for Christmas story time I will just flat map and I will say it works we will all be happy and we will move on to the next example ok so the next one is nice the next one is really powerful is actually one of the most useful design patterns I would dare to say in functional program it we can use we the mortals in our Java so what let's start this one I will be the northern or the repository which extends GP a repository of order in long this will this is of course spring data three more defined by active true and then I will build an order exporter which has this dependency toward the repository public file export file to given file name the first thing I will do is to create a file in the correct folder alright good stuff and then I will try open a writer fine writer on that file and I will write the ID and did header and then I will find by active drew getting all the orders I will map transforming each of them in the lovely CSV line right I'll get creation date right and then for each writer right in case I will then return the file now in case think I was wrong I will be make sure to send an email love the error very careful this is better driven development and then from it again yes trav it again you log it just to be sure now and no one forgets to lock it right this is a sign of distrust Tina and the other developers in your teammates okay there are several things they don't compile yet like let's use the log Lombok right see CL c4g after that good and then this causes problems why because you see writer writer this right method throws a lovely exception back at Beckett us an exception which is not expected by the consumer which is taken by this for each this happens very often when you have metals which throw checked exception you can get again use the IDE driven development approach but this is not something you'd like to see in your code this is ugly okay wow what the heck was this happened it was so nice before so there are ways other ways to do it you could either write your own function to do that a conversion of an exception to a runtime or you can use dual for that uncheck that consumer that will get a kill a check consumer it will get done necessarily deserves a bit of attention this this beauty takes a check consumer which is a consumer they can throw a whatever it wants and gives you back a consumer what does it do is they actually it wraps left checked exception into our runtime one and throws that back at you this is something you don't want to show to your children it is a function that takes a function and returns back another function it is near the apocalypse this is this is what a hack right it is something you will get really comfortable with after six or nine months of actively working with Java it in my experiences list good but then okay you need to throw a trigger stones okay and then let's see what what will happen now comes the changes now comes the changer yes of course the change the kids says that I want also to export the the users si user repo user reports what would you do so we will use now the oldest design pattern in the world oldest which is of course copy/paste programming I'm kidding I'm kidding and then just export or user file and then I will do something like username username and then suppose I search the user repo here I will find all the perhaps and then I will map from the user taking it's yet user s I think something silly right what doesn't work upstream three and then you get user name yes it's it's enough for the sake of demonstration but the point is here I had to touch on three lines three lines what I do next is this I comment out the but I also really do that I could be place to see how much code I need to change I take that thing that I had to change right the other thing is precisely the same infrastructure code that I want to reuse and then I will apply an old refactor an old principle which means to extract and replace actually this actually this bit of code is the thing that I want to replace in certain situations so the first thing I would like to do is to make it a method let's name it right content this right content method is now writing writing what the heck are these orders think then look what some people imagined they wanted to do now to pay attention class hacking time and now pay attention please do back to our what is he doing why why protected isn't is an anti world I mean let's this will extend oh my god I expand he is using the e word order exporter good and then I will just hack this is hacking how many of you are hackers here are you not allowed to say that right but this is hacking yeah and I need also to get the odd very positive here but you see if you have this works this really works you will instantiate this and you will write the users your instance say this year you will write the order but this is evil people were burned for this people were fired there were murders passional murders about this because you will just read on this file very comfortable you will see that this method is being called and you should support you should know intelligent displays a little I hear right come on this pretty little I hear saying okay this method is being is being a subclass it's being overridden but if you don't pay attention if you are I don't know you might not figure out that this goal really doesn't go there but goes there in another file for my sake so this is people rapidly realize how big a mistake this was and what they did instead is to leave that method abstract so if a method is abstract it's obviously that there is no implementation so you have to search for one right and then you can have a class which is user exporter for exporter I think you have class right no this is or their export that and then I don't know what we will rename it afterwards this I also do that a lot and if you spend more than five seconds finding a name for a class put something like that if you are just developing it it will take seconds to rename it afterwards but just the point to have this here this is abstract very nicely okay and I am overriding actually the right content I am providing the implementation really do any of you know what the name of this design pattern come on it is the template method in which you have an algorithm a big pile of code in which at certain point you want to call one logic or another logic and you provide the missing logic using overusing extension using abstract methods that get overridden implemented in subclasses this is a way to provide the missing logic using the evil I forgot to the evil order exporter now this would work and let's write some code right so class client code code and then we have a main here in which a Feinstein chain how is this this is actually just the exporter right now and this is the let's name it or the exporter it is a good name and this is user export right now user exporter good now if I want to export orders I will simply instantiate new order exporter no issues about that very easy and then export the file export the file name order CSV good and in case I want to explore the users I will obviously instantiate the other one no nothing fancy here but then then comes the Java 8 thing then comes the functional programming age but that age are not against Opie folks but sometimes functional programming really simplifies the thing what you really wanted to do this is to execute arbitrary bits of logic in this particular location and to do that you use the e word the expense word right but I have real issue with it with expense so to avoid that look what I what we can do we can say ok so what you really needed here to run is something which will write to a writer its signature is a function which returns void and takes a writer it is a consumer really so I will take this as a parameter it's a consumer of a writer which is something like a Content writer for example and this content writer I will use here and I will pass the writer in this particular functionality that I have here will do it stuff I'd no longer need to extend anything the desire no longer exporter really these are order export content writer perhaps even better export content writer good now I need to instantiate both of them but I also need to instantiate exporter of course I will never instantiate this in real life I will use some spring to do that for me but just for sake of fun let's work without pen here exporter equals and then when our colleague sport or dot exported file I will pass the filename that i need to use i what ii this one so this is actually the order content let's name it like this this is the user content user content and then look what we should do in the exporter past this comma ordered content okay and then for dot this is the point you will reference this function which will actually get to write the content right this is the thing that needs to be done here so basically you will pass in the missing functionality how to write the content in this particular case using an order content writer or an a user content writer you get the point maybe i may have still some type of here but you get the big picture right i'm just passing in the missing function that i take from another instance for example these days but now i have the same issue this has this yeah i owe exception what can we do about this we saw one solution with try-catch we also saw the unchecked stuff i won't repeat myself i will just like it all to close your eyes I'm kidding you it I will not say anything just silently you didn't see anything happened okay we are all fine but what's this right content why is it throwing oops you shouldn't be I think it put a foot wrong are you a sneaky truth oh my god I've mentioned it okay oh my god what just what did what this does release from Lombok of course is magic in code what this does is silently swallows your own exception in rapid inner in the inner runtime exception instead right good but to get the point you are using you are passing in the missing functionality but there is more to this pattern really there is this discussion very nicely I want you to think now in this scenario with using these two classes that we that you you see I want you to think how would you test each of them imagine to test this one you will just pass in here a string right right you will write all the content in memory in some string writer no files involved you will just take this string out of that string writer and absurd how its how is how it was done very nicely without any infrastructure concerns whereas here you will probably pass some mock some dummy content writer that just writes X right and then test that in isolation so this really nicely decouples the infrastructure concerns from the more let's say business logic or more elevated concerns more higher level policy let's say and I found this to be very nicely if you are doing either on TDD or if you are trying to cover very very careful your code with tests does it make sense okay so passing a block is really this pattern and I will see them all at the end but I have one more thing to show you this is a derivation extracted from the famous katha video store from Uncle Bob and it goes like this we have this movie class which has pipes to be irregularly happy new release it can be children and I have this final type inside of it and compute price based on number of days I will switch on the type and in case it's regular I will return days last one in cases nearly stays x two and in cases children re I will simply depends five otherwise zero zero three deducted from your salary right now tell me what is the thing that I know is you most in this example of course how can i what do you have to do with my celery leave my celery yellow why do you speak about my salary so what you should do you should delete the offending line of course but this doesn't compile anymore because yeah of course it's missing some return so it should any switch should have a default of course which will return minus one but no trap has a new illegal argument exception there you go very nice and then you will hope that when you will add a new type here you will hope to remember to come here and to add a new case do you know what is the most used development methodology in the world Venkat amendment mentioned it several times GDD Jesus driven development Jesus hope I find that everywhere I put that switch right so there you go this is a problem for me that I might forget to add this other case when I change this you know but then what can I do let me let's think in limited scenarios where it's very simple logic I will there to put this lovely method right inside the enum I know some of you may not agree with that because this is business logic what does it have to do with the type but you know I like to think of it as type specific functionality as functionality which is specific to a certain taxonomy to a certain breaking certain type so then yeah enums can have of course abstract methods we all know that that's what that's why we chose Java in the first place and River should return from regular hero should return this yeah this here and I'll return this this is x 2 and i will return here that leaf 5 nice good and now what's the advantage of this approach is that in the code looks like this return type dot compute price based on days and period this means that if I delete this I will also delete the illegal argument exception which is perfectly fine because it's impossible now to adhere a new a new type a new value without adding the missing function it's red right like it's red so I will have to provide an implementation which is good right again I agree if this bit of logic starts to grow too big it's no place for it to be in an enum in which case there is one more particular case what is the business decides that these two needs to be taken from the database that they want to adjust that factor later on what would you do but I'll tell you first for sure you can't inject anything here right you can do auto wired I don't know something here it won't work this is an entity alright okay so then what do you do well let's see so you start with an interface new release price repo which will get this factor from that 3/2 right that silly - then I have price service this private final new release price repository this dependency in compute new release price I will get the days in and return days x replicate factor you get a point oh yeah I'm getting you from the database and then compute regular price based on this will return this plus one as usual and compute Shilton price will return of course five thank you and then public in compute price based on number of movie times and number of days we'll switch type and then in case I'm in regular I will return computer regular price based on number of days right in case I mean new release I will just return a return compute new release price sounds like that okay in case I am in children I will return compute children price of this computer children price of this and I'm done right basically well not quite this needs a cost here okay and then this will this is missing again the the return right so we know how to fix this default through new but unless we type this will think oh my god I'm putting it here an exception with one word in mind right and we with we know that and we think oh my god what what am i doing wait wait I was I was trying to avoid that right okay so then what do you do first of all way let's clean this up a bit okay like this how is it okay and then let's I will not have any more methods there but instead I will have an horrible thing now I would like to excuse to excuse me for the things that I will write in the following seven over five minutes okay public final by function oh my god by function taking as the first parameter the price service the second parameter is actually the thing that gets into the method which is an integer and returns back an integer which is the price algorithm oh my god what the heck is this I'll take it to the constructor of course our import to be function and then comes of the beautiful part so folks again let's let's understand what we are trying to achieve first of all the deep logic to calculate the price for certain types of movie now just went into this price service right so I would like now somehow to be able to to link the enum values with the various implementation of these algorithms and I can do that how here I fight type price service for dot this this will and this is this is a tricky part really I am referencing an instance method by giving it by taking the reference from the class name this means that from this actually this reference is a big function that takes the first parameter the instance that this method needs to be called upon so this is how this works and compute new I messed it up this is okay let's come on here nope nope not again compute a regular price good and then in case we are in children we could compute the children price compute the children price children price children price I'm using Eclipse okay so then the point is the following so let's suppose this is a service a full-fledged service attack in general to wire with spring stuff okay then here this switch as before I will try to replace with what we've done just now so I will start with pipe I will take the price algorithm and then I will apply the first parameter is the instance on which it must be invoked which is this the second is this parameter the actual parameter which is days and I will simply return this again this no longer needs any illegal argument exception here to be thrown because this is pipe says you cannot possibly this is nonsense the you cannot possibly add a news value to this enum elders this won't work it won't compile it will ask you to give it a reference and what would you do you will come here probably or at least you will have a warning sign at compile time that you might be miss you might be forgetting to in to implement something people says here that okay is someone who could pass null here yes they could but come on I don't see off from developers very comfortable typing this is not something you usually do in feel comfortable about that right so this is the why I'm more confident and this is better than just hunting for the switches where I put now okay this really works let's try first of all I need to invoke that I need to construct the new price service I think it's called yes the repository here I need a repository this repository I will simply walk away with new release price repository okay plus and then I will repo I will when this when when this repo factor then return we turned an import from yes educated then return to D okay and then this service that I have here I will use actually let me yeah price service dot compute eyes compute price okay and then we need this type as the first parameter and the second parameter to be the number of days that I am borrowing the time renting this movie good and his runs gives me three four five nicely so how this really happens I'm going here really in this computer place this based on the type that I provide gets me back the function I need to call and this is we had Obinna in a sense because from here I get to the enum and from the enum I get back here but there is a price to pay but the advantage is that you can never forget to provide the missing logic but a bit of logic that you have to put there right good now this was about it now let's draw some conclusions here right what we've really done is we've tried to take the clean code rules and to apply them when when we jump on Java its syntax what we've seen so many people say that cling code is all about expressive suggestive names in this context a lambda is an anonymous function so you can imagine I have a problem with a lambda especially a big one like that that starts with an arrow and then a bracket and it pours ten lines of code I don't like that any bit of any any chunk of smurf of lines need the method so what is what I tell you is lambdas should be just one liners should not start with block and pour code there it's not the way to go if you search my code base you will not find any occurrence of that token arrow and then bracket nothing zero every time I have a fat lambda heavy lambda I extract it into our methods either in the same class I drain some item class in some constructor some static method whatever you do put it a name right then lingcod said no nullable parameters because if you ever get a nullable parameter the first thing you have to do in the function is to check for noone and we've done that and we've seen how ugly this pollutes our business logical if you replace noodles with optionals in all the places in your code this will translate so you should never accept optionals as a parameter to your function instead of that we've started from the optional and we've mapped we've applied the function in just in case there was something inside the box right in the same spirit the function should not return on all this you should throw an exception as soon as possible in case you detect some error condition not throw the dead cat in the others run garden right don't just pass ahead there you go there's a known there's a Schrodinger's cat you know there you go it might be no no you forget about don't do that instead of that just pass it an optional that it can be signal your color there might be nothing here and this in particularly is one of the most important point today person optional and it takes time to to to apply in on your codebase even in jeta's inviting getters from your from your area formula for entities so whenever you have a function which made it or nothing returned an optional instead of an all right this is very important now again there are may be times as we as we as we had in which you might wrap multiple times the same thing right just apply slash mapping get rid of the extra wrappings right just that now at some point we wanted to execute arbitrary logic within our function this happens very often plus we have this algorithm and at that point when to do this or this how we can do is take out the based programming is to add the bool and permanent we didn't even do that but you can imagine if I'm exporting users do that else do it the other stuff this is ugly this is even worse you see I think it's more than I did then copy pasting really then extract and overwrite and we've done that template method and we they are our witness are witnessing the funeral of template method I might say because with with with with with passing the block that Java it allows us so nicely to do the the whole template method idea doesn't make any sense anymore you just press the missing function there are some limited cases of course it depends but 95% of the cases you just have one missing bit of logic it you can safely pass as far as a method argument however the example we saw it's really is really more than that because if you look carefully this is the dis code we are creating this the writer we are passing in the writer and then we are closing and catching exceptions about that writer so this is what really happens is that the content writer is loaned an instance of the writer which it does not manage the real export format does not care about creating files I exceptions doesn't care about that just write the down format in this the couple's very nicely the things for testing you can very see the test each of the parts in isolation and I like that a lot good by the way we had this in place years already when GBC template you had that raw mapper right and we you press the Roma but how many of you know about the raw mapper about the gdb simple okay almost half of you so the point was you just passed in the raw mapper and which converted the results said that you did not close or managers create you just given a result so it is very nicely it lowers the burden on your reader another form of this is the in case you already have some bit of logic for example save page and you just want to measure how much time this took and you you sort of aspect I don't know is not very correct I'm saying that as sort of weaving another function around it right to measure the time you know t0 equals t1 equals print out these one nice to d0 or in case you certainly want to save your requests in a transaction okay besides its transactional we can still do that right execute that in a trans are in a transaction it's an ad hoc proxy I might say good but then we we stumbled upon a checked exception and this is good right because linkage cells to prefer runtime exceptions I'm not talking about frameworks here I'm talking about your own business project code you don't want to couple your code with checked exception you want to throw runtime and don't care about them anymore right now not accidentally Java 8 standard functional interfaces don't throw anything so you will have a hard time if you want to work with checked exception which is good my point here is to read through checked exception as much as fast as possible if you are trying to use Java 8 because it will make your life easier we can try libraries you build your own little functions or I don't know Lombok good then at the end with we very rapidly we schemed over bad specific logic how we can implement it in multiple ways first was the switch and this will get after the switch witch hunt day right where did I put my switches oh my god well then switched on this it's painful it's dangerous however we must say the switch are the simplest of all because especially if you follow these rules I personally follow these rules every time the switch is the only instruction in a method the first and only instruction and every of its cases directly returned as much as whenever possible right whenever possible directly return and not not accidentally this is this is one way java could go through if you know about that yep GAAP proposal to have switched as expressions not statements we could have we could have Scala switches in general but in a certain amount of years but this is a way that Java could go to then IOP didn't even cover that the traditional Tatem went on having the movie with subtypes for each of the specific spider I mean sub classing there but this is yeah I don't know it's maybe to complain it's too dangerous it's um it's too much of a surface statement it's too tough to start with really what we've seen it instead we've seen a trick you can put small bits of logic inside in the methods or we can reference some other methods in case those matters grow too big pull them away in another place and use appendices whatever you want there I would like to finish with the manifesto for not only object-oriented development how about this one I did not invent this I just found it look functions and types over classes purity pure functions no side effects over mutability composition over inheritance that's an old principle actually higher-order functions over method dispatch now give me that I'll give that back to you so higher-order functions and optionals over news but think about how you can don't use functional programming every this is not the way but learn to balance Opie and functional programming for your needs now that was all folks now it was how many everything it did this was all too fast raise your hand how many think honestly think this was all oh good nice the point is I wrote an article about this on the zone you can read it slowly afterwards it was all recorded and I'll be more than delighted to have a chat with you bastard after this talk on the website you can have you'll find trainings talks goodies other thing you can download and do follow me I posts loads of quality stuff each day right each day - one or two posts interesting but the best part is if I brought your stickers yes the guy VIP culture is based on stickers so I brought you all stickers and in case there are technical leads here I have an extra present for you guys so come further then take your sticker but please do take just one I brought several hundreds but you are a bit more than 700 I think so I invite you all here to take your sticker and brag with it right good thank you all folks that was all for today thanks [Applause]
Info
Channel: Devoxx
Views: 61,003
Rating: undefined out of 5
Keywords: Devoxx, Devoxx2018
Id: YnzisJh-ZNI
Channel Id: undefined
Length: 49min 56sec (2996 seconds)
Published: Sat Nov 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.