Clean Code: The Next Chapter by Victor Rentea

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

While it's sorta hard to follow him, Victor always makes their talks super fun and engaging, with lots of jokes and knowledge intertwined. While this video is long, it's a two parts video. The first part is a bunch of slides on the topic of Clean Code, Pair Programming and sorts, the second about a practical case of programming horror and solving it with refactoring using IDE knowledge

👍︎︎ 3 👤︎︎ u/Alxe 📅︎︎ Nov 11 2019 🗫︎ replies
Captions
hi hi everyone and welcome to the app of clean code the next chapter first of all just a little detail this is a sequel there was another talk of my available on YouTube already at devoxx Poland two years ago which had some content that I try to avoid that content and to create some new for this session this is that's why I call it the next chapter okay so if you like it maybe check out that other talk and see the missing pieces that you from this talk okay before I introduce myself I would like to start with a question when something is painful in you cannot simply avoid doing it so what do you do what do people do yes thank you that's perfect answer but the human way of reasoning is lettuce for tomorrow right let's postpone it next day you become a flea right and what do you do you delegate excellent but that's not the maturity of handling things what you should do as you said already do it more often this is the the banking philosophy bring the pain for the banking philosophy of extreme programming how many of you have ever heard of extreme programming raise your hand yes I love you I love you extreme programming doesn't mean you are doing sky riding while coding means to be a professional and they addressed exactly the four most dramatic pains that we encounter at work first of all how does it feel when you are in some code base no in you when you are on your branch for two months oh my god because you know the merge will come to you and what do they say don't wait them to two months for that to happen do frequent pushes yeah maybe even go for Transbay development if you are strong enough for that but try to avoid branching for a long time because that way that will inhibit refactoring on the main branch and the other one so it's all you will just freeze the codebase actually next one after you do the merge they call you for a code review and how do you go to get your code review I'll tell you like this so the point is don't wait for the inevitable but instant ask for help and something is just 1% over your your your your comfort zone just ask for head step on your pride and ask for help ask for a pair programming not all the time if you are lucky enough to do it full-time perfect amazing but if not when the problem is just a bit of your over your head realize it recognize it and ask for help the third one with the code base which was never refactored how does it feel fair what do they say don't ever push code unless it is worthy of added to your CV it's not written literally what they said but this is how hard to put it it is worthy to be added to my CV this is how I write code if it is you can push it and the force and the words pain of all you open some code then you find exactly zero unit tests how does it feel oh you won't dare to touch the code because if you break it it becomes yours you know that so then you leave it like that they do this first even if you don't do tests first don't ever push code but never put code invisible sight unless it has a reasonably good unit a suit good are you agile again where is the agile right right because it it runs fast and in some countries this is how agile is seen exactly and this is the business right so this is neither a jar was actually supposed to be a just stood for those four technical practices at the beginning extreme programming was the purest of all and the those who fight today for to resuscitate to to recover those lost values are under the umbrella of software craftsmanship and ISO boots downstairs software craftsmanship interesting so these the guys will fight for technical practices for for for a code of ethics like they like Hippocratic right these guys focus on reflecting on how you work now in this happy turn I'm Victor I'm from Romania I actually ii have a champion a romanian i'ma lead architect a basically an internal coach at IBM and I do this weird stuff I do programming I never wrote code alone for the past ten months in production I always have someone with me sometimes two or three more refactoring you will see the way that would you factor as we go I never sit back and then now let's refactor no as you type code you you burn in the refactoring and EDD I don't claim I do TDD every day accounts but I do at least once per week whenever recognized for it's a problem don't hesitate take out the bazooka right okay now let's do this one but if you can isolate it and if it space to do that now 802 get a point that I'm really serious about this stuff you can read here bucura software craftsmanship community so I'm pretty fierce about this stuff and I've been talking about this for a long long time already and the the point of this slide is actually that that I have a website that's me the button yeah that's the point you can find the recording of the other talks that I gave until now I have been training quite a bunch of people and this is actually a synthesis of the clink of Turing that I deliver and many many many many many companies already I teach I also teach a variety by the way but of course the the to get the point the clinical training is among the most I've been talking about these top research you will see today for quite some years right now the point of this slide however is in the corner for you that I post regularly on all these three social networks altogether things that I learned myself honest things that I found interesting stuff now let's begin Plinko about the elaborative structure will there be some slides for the first part of the presentation and then for the second part will be some live coding cleaning up the famous video store example from up your father's first book together linker does one thing well says the inventor of C++ C our pirate finger sponsibility principle clink Audry's like well written prose it was written by someone who cared right when you open up a message you don't fall off your chair when you read the implementation you expect to find the things that you find when you open something up I did you don't the code doesn't shock you and you four can write this is the famous quote of Martin far anyone can write code that the computer can understand and we all enjoy this so if any of you have memories about this but a few people are however able to write code that others other human can understand this actually leads to the east so you're a unit of measure for a clean code which is I won't spread it out is just that you're good when you could reveal it it should not annoy you should not make you swear okay that's the universal clean code measure why do we care why it has the cost of software is not in a Danish development but in its maintenance quite a lot of cost goes there as you all know so the point is that the past which is very simple for us to do to that today in several years becomes becomes harder and harder leading to the inevitable rewrite or I don't know breaking up of things refactoring Big Bang I don't know this is called code mess now this is this is our bosses Jabra because this actually buys us a job right we will have a job tomorrow because of that right but when you realize that you turn the two that you read ten times more time then you get to write these kinds of make a click right oh so when you write six minutes code someone will have thoughts about you for one hour and thirty might change in your attitude now a simple rule at vowel rule when you push something don't limit yourself to just doing your simple if don't just put there your little additional code just clean up the mess that you finally clean up the entire the entire garden when you leave from there now just to make things clear refactoring is to simplify existing code a simple is not easy as the richest puts it there are no catalyze toxic and you can watch simplify by the while preserving the behavior the point is the move that you do should be safe don't cut and then I'll under under completion failure start refactoring try to copy and maybe old called the useless grayed out and then kill it just replace the implementation don't do big banks and use the IDE we were yet to that all I always try to do the tiniest possible steps and III was heard someone saying write down when you start working write down on a piece of paper with your hand right down the commit message of the next commit can imagine doing that what's the nest that makes little stuff to do right which I wanted to be painful for you write with your hand be sure to limit yourself because otherwise you will find yourself straying doing Adam don't know what other stuff you find out and also all all is always this is very critical it very very hard to start refactoring code that you don't have a test fit for but the point is if it's all if you're talking about legacy code you will build the unit test as you refactor it's impossible to cover with unit tests obnoxiously she ugly code completely ugly code that's the point why do you refactor you build a scaffolding you build the unit test we will see now we all enjoy coding right but many teams that I worked with when we talked about unit testing they thought about this why do you think they thought about sonar because sonar had a rule that you had to had eighty percent cover this is we are live streaming so this is wrong totally wrong and generally can you figure out why did I put here a confessional when I'm talking about the refactoring sprint it we are all waiting for perhaps because when you request your boss to give you a refactoring spring you are actually openly admitting that you did a bad job if it's your code I mean right that's father I have sinned all right I want to repent give me one sprint to refactor this is not right that's why I should never have these three post-its on your on your board never if this the same use case now is our story never separate them never kill those two d'Ivoire indeed better in the way we work otherwise you will come to do it to work in such a place dominated by fear and error and that's not what you want but you will say okay but we don't have time I know I have seen so many different we don't have time do you guys know what a deadline means in Webster medium Dictionary a deadline is the line around the prison that if you cross they can shot you without warning that's the first meaning of deadline the second one has to do with the time that we know so we have a deadline to meet right and I understand the respected life is life but tell me what do you do after the delivery date or the what do you do afterwards hide drink not quite what you should do right now what what are these guys doing can you figure out they are doing mob refactoring aha or if you saw a terrible code just before the demo just before the delivery be right after did deport that point the next day you don't start planning you don't start it respective just one hour I'm boxed in a single room all the developers together in that team all the projectors starting to kill the Beast if in one hour we can figure it out at least you can set some goals for the entire teams I've seen a team of four developers first of all can you imagine a class of thirty thirty-five thousand lines three five zero zero zero no I have it on my lap it's not my code but for developers I know three years working in that class just pushing in the right direction managed to break that class in hundreds of classes 300 or something all by team effort without any big bank just just imagine yourself every one of you if you run if you go out that bad door taking the the wall in one day there will be a hole in two days the world will start cracking in one week but we imagine the power of the team that's the point if you want now you can seek you can fix it at least you set some goals right hmm think oh now what I find in my in my trainees everywhere I go I think God has a lot to do with emotions a lot to do because we affect the education system especially in computer science tries to to make us forget that we are humans we are working with bits right if it's not right we are emotions even animal and that's most visible in when I'm talking about twinkle or playing card in you will see that every time he means to listen carefully to your emotion and use those hints that that that instinct gives you very very important then it's also about teamwork as I said we can turn down this this building and also about attitudes very important to keep a smile on your face to keep to make coding fun but if very very important to be happy and relaxed and be willing to because if you have stressed it's over so what I will have to ask you is all of it what do you choose to do what do you choose to how do you choose to behave because I find that the content of Clint or these pretty obvious and pretty natural to most developers what makes the difference is that what you choose how you choose to behave for me the trigger was this website if you know it if you don't maybe go there and see there are those videos super interesting they they took me out from this hmm good a great man once said that with great power comes great responsibility now what this means is the Broca's area do you know what Broca's area is a place in your brain which has to do with speech with naming okay naming one of the two hardest problem in computer science so it's the simple things first classes are now so every kid knows that nowadays but why when you choose those names be sure to avoid meaningless particles like tell me if you see order if in order all of those three sitting right next to each other in the same package what do you feel what's the difference this is like it's like what like refusing your power or that brutal or it has feels motion okay may perhaps you then it would be better to stress out some interesting intelligent names like details with 200 fields and family with any fields only first search for any name is use especially for the entity or or or the be announced have three to five options before putting a name it's very important for yourself exerts practice right I want you to already to enlarge this disease okay good now in this happy town let's talk about I customer service what is it it's an interface of course oftentimes right next to it there's a class named customer service brilliant or the alternative or delivery simple and order service interface this is not I don't see that often this nowadays but the point is very simple delete the interfaces interfaces accept three cases when you package the interface in a jar and send it to someone else we rarely do that anymore it depends if it's a strategy pattern and you juggle with the implementations and runtime well maybe you weave some decorator over the existing class all basically meaning polymorphic method dispatch beside that runtime which is a valid case very interesting very clean and at times or has to do with depend the same version if I don't hear about the producing version come to my talk tomorrow it clean architectures it has to do with an onion with the most advanced architecture in the world the onion architecture now the point is I want to do this the interfaces is they have only one implementation in the same module simple as that right again it's not not any more such a great disease that but now a hard question if you ever blamed some code who has ever brain some code some yes I do you want and now keep your hands up keep your hands up and keep them up in case you were the author of that code yes those of you who are still standing with your hands up those of you have realized have realized that when you by the way this is something that my father used to tell me when you blame someone with one finger three fingers are pointing to you very interesting now those of you who have your hands up until the end did those of you had that moment in which that they realize that they actually forget in time that's some transcendental moments an epiphany but first realize that you are also stupid then you realize that probably didn't review denounced the code and thirdly realize that the design of Friday on Monday it's a fuzzy already not to mention three months from now right and there you are trying to understand some code right this is what this is your life this is develop or the normal developers life is world revolves today because of legacy code that you have to understand and tell me how does it feel when after reading for a while you get the point and you put that line that you have to do one night now let me give you some more details you spend one hour and two hours reading the code one minute to add the code how do you feel I tell you like this but do you know many people just in theory resist this frustration and then this leaves two-burner other thoughts when they act it's actually a hint from your subconscious that the code was hard to understand so what should you do to release that that frustration of just reading code it was hard to understand because the name didn't spoke for themselves what you should do is to find a better name but sometimes you have this feeling oh I can't be smarter than the elders you know be so what do you do you just leave it like that Who am I so the point is don't they the idea you are not paid to write code you're paid just to understand the business problem and to solve it in the least amount of code in the in the simplest possible way I also have a say juniors are super eager to write code seniors can't wait to delete it so rename it rename it 10 seconds with your ibe very very easy and very rarely it will fail when it will you will find that you will figure out in time multi-floor actually has the has a name for the city a is it's called comprehension refactoring when you figured out something and you want to leave behind some landmarks from some hints for the next poor fellow who will try to understand that so now let's talk about the gap the gap on one side is this fellow with the tie I don't see much of you wearing ties so it must be you and there's a gap you know there's a bridge there's a gap the bridge and one thing that we need to do is to to strive to make the code as close as possible to the domain how many of you have a minute with demand driven design violent even is that perfect no there's a an amazing book named the first the first 15 years so free book and read it ourselves all the basically it's a set of essays of successful diamond driven design projects for the past 15 years all of the O's a say is having common the fact that all the projects that had benefit from the DD actually stressed a lot the ubiquitous language focused a lot of crunching the domain of understanding all the subtleties so language do you like overload no it's the same method with the same name but different sets of arguments yay more functions less names not about duties like I can't stop it's just is too tempting for me to put this read util classes what do you feel feel huh good now right before a very minimum at least collect selectively right now but you will as probably working what how else right do you need to put some stuff in utilities now they just imagine a method which takes two parameters one thing you could do if the method is general-purpose doesn't have any domain knowledge you never need to mock about one thing you get the rectum turn the metal into a static utility right very simple very very very very trivial but there is one more step you could do which is somehow whoa you could create a new class with the two necessary field and which has that method just imagine the the SSN and a social security number or a class and the so-called micro types or the very very bits of one two three fields you will see today such examples of data with logic embedded world in them value object which actually means exactly that to deepen your domain logic to discover new concepts in your domain model functions functions functions it's all functions right oh my god whose also should do one thing let's start with with the classic the function should do one thing to do it well and should do it only says uncle Bob who wrote the clean code book and then they should be small how small should function be three lines at which point how half of you get up and leave right five lines of code why actually uncle Bob changed his mind afterwards he said five is too much you should extract you to drop any acid on me on Twitter the point is fine why because the idea if you have five lines how many things you can do really if you do a try-catch you're out of line so what would you do you would call other methods there it is but the point is five lines method you get the chance of naming it just imagine now a function of 200 lines how would you name it execute process do all right that's not fine now I agree that the normal interface applications in which you just move data from here today that's what and enterprise applications do moved it alright when you move data search research tool is kind of weird I mean wow really I'm just get set Get Set Get Set yes sir so five is a bit too less for me but I would say less than one monitor and when I say that I don't need you to rotate your monitor don't just to be clear don't now even so did this is just a number it yeasty that I do today is in case I find the methylene that I don't understand in five or seven seconds I crack it I don't hesitate and what would I do I will break it into pieces I will extract method yes little little methods and I always have here at the belt a knife right this knife is called extract method and I said I don't think extract so this is one of the two things I would like you to take out of today's did that we rename every time and the second one is extract as often as possible extract methods put your hand in the code keep them there don't read code like this no I'm scrolling oh no I wanted to be like that in the code right I always be be able to take out code in to extract the method and extracting a method is just the beginning of a beautiful friendship you might then extract the trust who knows maybe micros okay now people some people are concerned about this fight between performance and entry code when clean code literally actually means small methods that's actually one of the key points of clinical but in Java and any other in any other virtual machine language smaller matters run faster if you don't believe me just google sheet just-in-time compiler if you have a complex message it might even say in my team actually brings in the console this method is wrapped up I can't optimize it right you don't want to do that you want little metals it's just can't juggle with and compiled to native code very very easy very very optimal but other of us so some other of us are scared by small functions because instead of manipulating just one book function suddenly they have to juggle with little sanctions and trust me you won't even a recall the names of those 20 functions there instead of having one function hit there you go 20 when you named the last one you don't recall the name of the first one so you as an author feel like you're losing control but the whole team will think why because the methods you extracted don't are named like M 1 M 2 M 3 no they have beautiful names like send activation email to customer or remove all past canceled orders honestly if you don't understand what this function does I don't want to remind t a my team simple is a very very very very small matters which descriptive names but I want you to think of the atomic bomb of the of Richard Feynman do you know Richard Simmons was one of the greatest teachers in the world ever absolutely brilliant he was at some point working as the link for the article this was working for the other item bomb and he was sending letters to his wife but guess what the Secret Service's intercepted that those those mail obviously but then look what he did he bought puzzles he wrote the mail on the back of the puzzle and then broke the puzzle apart he heard his wife assembled back the message read the message and sent back in the same way after several such exchanges the CIA sent him Seema mail please stop doing that we have one guy assigned to do just that for you please stop doing that right but I wanted you realize how would it feel to have a puzzle and to want to paint something over it a little bit weird but the first thing you have to do is to assemble back the puzzle so if you want to every factor or a refractor optimize design redesign completely some codes which is broken into many pieces I would in line everything back I will print it in a big piece of paper big one like the build right Larry by heart because why because human brain works better with monoliths with with blocks of text instead of juggling with hundreds thousands of names and after I'm done painting I will take the puzzle oops break it into pieces again extracting back the methods while I leave this is how well factored code can be dramatically but I mean dramatically redesigned now does this make any sense alright first in line back if you have to dramatically change the way you're doing things twist it around and then break it into pieces now let's talk about this method call the first thing you have allowed to ask you is what is parties which point you should ask what that is for crazy parties who everyone knows what policies is a city no it's a street it's a street what's wrong it's the Paris street in the city front albergue who knows what happened the method call had too many parameters and you got tricked by the order and them semantics I know IntelliJ displays you the name before the parameter true but then in dimension 10 parameters you will probably put them on different lines right-aligned them nice but the first thing you should think of when you have many parameters for a function is obviously isn't the function doing too many things and he will take the knife and chop it into pieces right then just imagine the function taking those four parameters turning around and calling another function I think one more parameter have you ever seen DUP yeah it's a pleasure what would you do that because then because the function doesn't do much just call the other function what what would what would you what would you do them you could try to introduce what is referred to as a parameter object or a DPO data transfer object or value of the point is a class with feel that you instantiate and you pass around to reduce the number of total parameters I will probably make some address or full-name we will see in code quite soon that's one option you can do another thing that might happen but I would like you to actually I will gently jump to code why not let's start with this one because it's more this so he has place order which takes FN and LM by the way this is L you can figure out because it's higher than n surah see right good but do you expect that because it's our by the wave to to to your surprise I'm on IntelliJ now for so one thing you could drive seeing these many parameters you see Wow let's take it let's package them in some class so what could you do you could try to invent yourself a new class I will write them here name let's say full name for start which has some private final because this is the best way of doing it string and it will be more generous this time first name right and then probably last name and then we get in trouble your constructor of course and then you need the getter first name and last name good get lost there you go this is my little little class full name which has to be this public and then I instead of passing these two here guess what I will do I will say new full name right grouping together bits of data that move together to my system new full and then I will press what it's red what should I press alt enter but instead of this I will just pass here what su name myself so I'm just packing packing Jake together and passing the full name down at which point here I should do full name dot to get first name and just to make things compile again full name dot last name perfect and here is full name dot yet first name in where and this is full name dot last name perfect and I'm done right but if you would just do that a new class for every single method call which has too many parameters this would smell like over-engineering right I mean what the heck you're calling demented you are creating a new class to call methods are you insane what you should do next is to try to see how much code that new class can simplify meaning you will search full text search everywhere by first name and you will see if you find first name anywhere else in your codebase and guess what there is another method you didn't know about that takes first less than X what would you do now but you have to have a gutful to that this is way out of three out of your trail this is way out of just fixing your bug right this means touching other people's code so what would you do here very simple set full name full name of course with the necessary refactoring it will take a while but before that you do have to meet you to have a coffee relax and and go but then you see that the check is the same chick what the heck what does this what does this do really it requires at least one of those are set what could you do where remove it move it where move it in a new class that's the point every class you create is the host is a possible host for logic so there you go you can put this here and there you go it has some validation in your little value object at which point you don't need is there you don't need this here however you still need to check the full name isn't null right unfortunately we are in Java that's so basically you just have to do this both places but there is no reason there is no reason of duplicating the valid being federal validation of the new value object so you get you build a home for the poor for the poor code without a home that's the point but then you search again because that's why I told you is it need to search again and then you realize is that there is this stuff what is this stuff wait wait wait wait oh my god oh my god oh my god wait that's Oh would it be the holy entity oh my god we are talking we are being the big game now wait a second what I mean holy whoa whoa what could you do [Laughter] sip some more coffee and then put the candle here full name okay and you guess what I'm just sketching stuff yes I mean GPA you still okay and full name and then full name here there you go full name full name and just assign this here and guess what there's duplication again that we can remove or hooray so no more this for you equals no so in which place you will have to do a decision at this moment this is fully what we do whether we the sectors and the constructor leaders kill these but the Gators are interesting what would you do here hmm first of all let's make a getter for the full name and then here is some code which uses the get first name and yet last name of the holy entity what can you do here you I will call my own data bill guys no folks no notches not just for in Greek mythology he fell in love with his own image in the lake NAR cheese likes his own posts marches call his own Gators but why am i doing this because at this moment I will inline this almost here in line did it work yet first night no it's wrong I'm just looping around I mean full name you're laughing what'd you do get first name good and then here is full name at which point you keep you in line this Gator guess what you'll get yes I know imma get right and under now with in line this everywhere everywhere and there you go it could have been letting this improve it ah good and bad yeah doesn't quite work like that but you get the point I would just kill the others just leave these just imagine that there is lots of code there right and what would you what happened here and then destroy against me there you go and you will in line this one and there door every I will now use the but wait a second what is this really yet full name gets first name plus whoa this is a logic that could fit in the value object right it's only getting data out of the poor full name so what could I do public oblique string I don't know as corn cotton nated how nice this could have written a util metal right home now first name plus space brilliant plus and I will stop now because it's obvious for you what happens here right you're smart fellows your figure out concatenated will be called here and I'll be able to store now is one of the reasons I switch to intelligent actually yeah yeah fun now one other interesting example is what happens if you have that was what it's all recorded so you can look after again what the heck happened it's okay and by the way we have a get I forgot to tell you but they were the way at the end there will be a link to a git repository finally this will be named in a capsule eight forums ah but I forgot to do one thing oh I want to tell you so suppose you want to simplify this even more now you will select all these with one hand right click refactor extract didn't work it has to put my my two hands again extract oh my god if they if they have the trouble to implement that it means if it could be useful forget what city street name street number and that's it it's it's an address oh my god good thank you not imagine this is all the work that we just did hahaha one thing Phil I will address I would pick the pain of doing that I will explain my code variable so imagine everywhere where the function was called now there's this parameter object happening very cool and I will let you dream on for the IntelliJ hey why okay so now the next one let's see this example this is a bit more production we are validating let's suppose you are not using the annotation stuff which can get you into a miserable health well let's say m1 we validate and really giving the parameters and errors just what happens inside add generally can you feel it I mean we are passing a parameter ergo what is this fellow does adds my parameter what do you feel you expected that is actually a pattern but the fact of adding something to a parameter to mutate the state of a parameter is the evil beyond reason I mean war so then yes what listen and you figure out this map this matter but the clothes smell that you detect is not there perhaps but is the fact that there are too many parameters at m3 what is the good name so they've chosen and then finally one there are four too many and then what do you do you realize all of the methods in this class have a common parameter named errors what would you do first of all I would like to clean a bit the code and move and move a bit up this parameter to have common form this changes occurred everywhere where you use if the method m1 was called in many many places change signature will just change it everywhere there you go now did I speak clear that I have the common parameter what can I do I could do by the way you notice here spring spring and then I should whoa let me have some guts whoa what are you doing poor fellow this is the thing that my friend what are you doing you are putting state in a singleton what is just wrong oh-ho no this is wrong so then well let's see what would the ought to disallow me to do if I had guts I could do this select everywhere and just delete the parameter now everyone will be using the internal data side internal state of this instance but I will need still the Gator but please don't ever generate getters just GE right or D error to be more specific yet just just and don't in the art in certain generate all of them no just selectively one by one and just here what happened well this needs to disappear of course but since I have internal state I can't possibly do this I can't possibly do this this should probably as they recommend to be injected to the constructor then guess what this will disappear now I can't possibly all to wire this can't be a singleton anymore so what should I have to do to use the word first it was the word new but you guess what if I chose to use the word spring touch will make me suffer why other dependency what other dependency I need to take this I need to inject in me the dependencies of that guy whoa this is weird but because realized is not that simple real life is like that can you can you can imagine what happens injecting there all the four dependencies needed for the classes you want to instantiate oh my god but for this simple example it kind of work but again I'm stepping out of the framework I am bearing to take the red pill and I'm doing new and I'm if I do this I can use again or OPI can have objects with state it's not a singleton I can in Spanish it is myself nice this is nice this is one other way and I should push this Opie hop sure now this factually other folks I don't have anything against frenemy springing for fifteen years but the point is at times you may pay to and to do or open the what references I have grouped common parameters in fields of a real object now let's talk about remove orders customer falls to how many things is does this function do technically for right but not being practices result in three then the point is when the function has a bool and parameters first of all not without asking I know that none of you have created the boolean parameter upfront just new function gets born and you put a boolean you don't do that often directly from scratch so the conclusion is the function has changed in time why did the developers added the buildable and parameter because the function is humongous was huge and they were scared about the function and they put that if you know so this function and know is developer because it's changes and people are afraid of it it is the perfect candidate to start refactoring now in this regard what would you do if customer could be null you would check for null yes what two more things Haho with wait wait wait wait where do I fight corrupt data where do I find the moon or negative ages or hi Donna where the heck do I fight those and this leads us to new wars where do you fight corrupt beta if you'd super if you could build a fortress in which to hide the precious code you put inside all the business logic and you will leave out all the sources of corrupt data even some database can come on uppercut moon why because you know updates at noon well the entry puts the security check decisive programming at the boundaries in order to gain trust inside the fortress you don't need to grow on the street and are even noon don't do that the check should be done at the boundaries but what is some for some place there is a noodle coming what should the protect the bodyguard do call the police for exception out right but now do you know that noone can have business meaning however they consider that that's interesting nor can mean something for example a customer with a new member card is a customer without a member card in which case what would you do associated what sell him a membership huh you're a failure you know if the null has meaning you wrap it in an box in an optional suppose you'll get to work in the morning at the raintree of your houses the box inside there might be a cat or not there you go you make him wonder what's that that's the point that me don't just hear the news don't return no now we might end up that's it that's that I need to we might end up having letters that return optionals and this my friends means game one this means you've eradicated the most frequent bug of all because from the origin of the data in the Holy entry comes only optional Tim come on optionals possible you can't you don't see new anymore basically good now that's what you feel it happened to us just now if this method activated the customer when you call it it activates the customer what do you feel cheated yeah you feel cheated that's the point and you probably would okay so the point is these are side effects which are necessary but can surprise the reader so you don't want to shock him right you then what do you do you make them obvious 14 or 13 years ago when I saw this method 13 I was like young the only thing that I could think of is say at least say check and activate customer it's outrageous this does two things it screams to us but check and activate customer at least you know mind your head for the next developer this actually takes us to an old old old old principle from Burton mile which says command quit don't know no CQRS command query separation seek us if a method returns void why would you ever call it well for short not for the result right so you would call a method that returns void just for it to do some side-effects myself you want to so basically methods that return void are allowed to do side-effects but if a method returns you a result ideally that method should not change anything in the world okay this is a guideline not the rule right it's but if possible keep side-effects at the boundaries and leave the center pure now this is a pure function a pure function is a function that has no side effects nothing bad nothing changes when it call it and also is idempotent meaning calling it twice with the same parameters gives us the same result working with pure functions is a pleasure like holding a teddy bear it's so warm you know I mean you just call me to determine if something fails I call it again is brilliant so write as much business logic as possible as pure functions so as more than three parameters no boolean short methods long names no optionals know how many rules right how many guidelines now Uncle Bob says that even he when he writes a function they all came out they come out long and complicated with a lot of indenting and nested loops but then ugly ugly but then it doesn't he doesn't stop there it massages it refines the code splitting and cleaning and changing him afterwards I don't think anyone I don't write them clean from the start I don't think anyone could this is super deep so when you first write your code it's a mess and this leads us naturally to the Hat metaphor from 10-bit who have you know about the Hat metaphor no hat metaphor it's super deep super deep I'm so glad few of you know it first attitude that you have related to your code is the writing in which you want to make it work as fast as possible the typical thing thought is copypasta but you just assemble the chaotic like India Desmond Rose Myrtle is just writing code but then you need to after it works for the first time someone smart said that moment you start investing in your career because at that moment you should lay back put the detective hat and look criticize our code and then try to make it readable see it through the eyes of your duck at least try to explain it or at least try to read it again for the first question is should you should think of it what can I believe for the code to still work that's the driving question right but there is praise for the third hat and the third has my friends is the gangster hat you take it out the machine gun and you try to kill your code you technically try to break it now what sort of a human being could you Bree to raise some code raise some code and then stickin a finger in his eye let me see do you resist at any hideous what kind of sociopath could you be to just kill your own creation you just wrote it what kind of animal that's the third hat you need to learn to wear people I heard actually try actually we're physical hats to further themselves into such a mindset it takes years of practice to be able to switch the attitude towards your code it's very hard because it has to do with feelings again if you do have Oh Opie if you read this backwards HP o-o will never miss struct I do type destructive so it was a pleasure all data lined up in memory they are trying to do it in Java for years already the data classified struct beautiful you can do struct in Java that's one particular tasty one which has final fields right you just made one just now right beautiful beautiful because if there are craters where they remain valid easier to debug trade safe and many other benefits very very very variable this is one thing you should try to do next day tomorrow every final word you say it should count as 0.01% at your annual race and I'm kidding but is very healthy don't do as much final fields as possible in this will naturally lead us towards function professional programming I just want to quote something I heard yesterday pure function is what you want to do side effects are what you are being paid to do very nice talk from Ross awesome so the point is this will lead us naturally to a world of functional programming but there is this other kind of point again the love remains with the immutable but there is the other kind of data class you know it right it has private fields and public getters and setters some people call it the object assembly language here yeah yet forget this is nothing capsulation this is nothing less than a class with public fields technically speaking if you generate all the public fields or public access receptors you wanna make the public field analyzed tomorrow huh then opine one slide forgive me it's for for for for doing it this one sighs if your your car I have an old car when I am at the red light I'd put next to me comes a jeep right that guy looks down at me and while I the engine of his car shuts down no the start-stop cars I am now this car has three states while mine has only one on so basically if my car exposes the angel start with a boolean or as a setter like it couldn't change tomorrow but if it were if it only had start engine under the hood tomorrow this state can be an enum with three values myself without altering the API basic stuff but my car when when it tells me something it hides as much as possible in my in my display it doesn't say to me how many liters I have left or how many percentage fewer no it's it tells me how many kilometers can I still run but you know why because my car just as I do hope that tomorrow when I will come to her to do it she will say my master I am now hybrid and will smile at me if my mum is overnight it imagines such a miracle and you come to your car and it has a socket it thank you lord let's imagine that now if your API would be getting estimated remaining kilometers the APL shouldn't change right doesn't need to change it will display the same information based on that refuel so that the point is that the implementation can change even if my API remains thing there's the essence of our P to minimize the API API one one reason to do that clearly is when you write your own library or framework this smells like pride huh so notes for it this is why actually 1.0 burns right because if you know that tomorrow you'll find a bud you will beg the things using your library to upgrade to 1.1 they will do that and then their code will turn red and what will they do then undo the change and stick to 1.0 and you see that's why I want to do OB aggressively extensively in libraries or frameworks but also there is a more common usage of our pin now in day to day work when you have a complex problem to implement let's suppose some export files right you could create your own CSV writer class now this is actually an abstraction over writing the CSV takes care of say of quotes of semicolon or return or end line we their fault or Unicode stars it is hidden under simple API the video tempura on complexity that's one other reason to apply or opie its recent comma is method but then there's this question that there's this question there is this other kind of of code other procedural code no procedural code lots and lots and lots of procedural codes so then what do we do how do we keep it simple how do we keep lots of procedural code simple one way to do it is to is to group logic into classes which brings us to the famous order service you know it you all have it that's the master user service you all have a thing 80% of your applications have a user service or customer service tell me is this a data structure no is this an object encapsulation information hiding no it's a container of functions anymore is the file full of functions there you go so basically we use what the language gives us classes to group together similarly concerned functions and logic in their regard then we get to this problem we have the procedural para being we have the OPP robbing and we have the function and program plotting and people have themselves which is best huh and they have 50 shirt I'm a functional functional programming activists I know people were very passionate about one thing or the other with what I in they asked questions which one should we use my experience with this I like all of them in the sea of now forgive me for this file this is overly simplified perception but in a sea of procedural code that naturally results from the specifications that we get we want to build a long Z here and there with our opt for those use cases which are more complex those 20% complex use cases but every now and then we would also like to build some function to use some function at programming the functional problem at least in Java is more concise and that's one key reason actually not imitability reference transparent not short per stream map collect please what the heck it's more concise so I would use functional programming wherever it is simple and makes my code a bit more compact if you want to do scalability and to want to send functions to spark again functional programming works perfectly and is also a bit more easy to debug in apparently less bugs however debugging higher-order functions in Java is a mess if we ever did that we when you step into you running seven silver class seven classes it oh yes just a bit more formatting and comments then you see the sunglasses so how's monkey keypoint who here has seen matrix mothers those of you who didn't tomorrow please do tonight please do matrix is made by us for us it's a programmer movie you have to see it sunglasses let me help you a bit they give you a broader picture it's a planet right wake up wake up wake up you are not me wake up in our project it's all about teamwork no one of you can hold can make an art project survive it's all about teamwork only you do we are we shouldn't be coding encrypting your code what what did you do today I encrypted some code no never you need to communicate with the others so remember people wearing will recall your name when they click annotate 10 times more than it took you to write write write literature the simplest possible code that works look da Vinci for Italy simplicity is the ultimate sophistication there is nothing more complex the student that to make a thing appears simple by taking the right decisions to go in the simplest approach no problem so write literature children literature and in case you have one they are full of energy yes yes write the test write a test invest there write a clean dry fast non-overlapping significant unittest or build a testing framework to wrap your your project entirely there's a super interesting approach I mean instead of slicing it's very very very very thin with mocks sometimes is more mature to build a testing framework around it James Copeland talks a lot about this it has a famous paper about why unit tests why most unit tests is waste is a controversial paper but it's very full of insights also there was a famous but contest but let's same challenge between Uncle Bobby Thunderman Kusa they both wrote code using TDD Chicago School versus London school at the end the Uncle Bob creates basically an entire testing framework at DSL testing framework the code is available on github publicly just google TDD sundar non-coastal in uncle bob is amazing basically he built a testing framework of wrapping the rest calls completely not mocking through it because if you test it around it you can always refactor the internals without changing the external make sense it's deep it's very best but just Google do you remember the first day driving John so without an instructor he lost me there you grab the wheel you have two t-shirts with you you know why but you you did the terrible mistake of taking your girlfriend with you she's right next to you so start the engine give the signal you have to turn left right if you're if I make you tea and then your girlfriend turns on the radio you just can't take that other pressure more than the fact of turning left you remember I do this the first day you drove alone okay now in years after one foot is off the window cellphone you're you're scratching your head these cars go straight okay and then you change the gear with the elbow why because you've developed some reflections this is what you ought to do how many of you know Mortal Kombat Mortal Kombat many of you know those special kicks right so for Christ's sake master your IDE learn those shortcuts if you are an intelligent install that plug-in keep remote Eric's I'll give to you as or even in the break I think it's easier shit-shit with circuits that you don't know but you have to know the point is to sharpen your reflexes because when you fight the big guy you shouldn't be looking for this gear shifter right where is the steering wheel no you should be clear pack and why are you driving with one foot of the window because you know where the dangers come from you develop some reflexes this is that what you need to do with your IDE also I want to tell you a little poem I wrote myself when it's red yellow blue or gray our tender will save your day haha but to be able to be politically correct let's rephrase it control one will save the day control both will serve the day get it is there any Eclipse user here still ok control one visual studio code or visual studio control dot comments to code from the big guys this is a refactor Marta falconback December last year second edition approximate code the chapter was named totes Mouse and there there was a section last section I think named comment and they went like that summer that we not that we have something against comments no the comments give nice floral scent to the code the problem is that too often we see we saw it being used to cover other smells yet so the point is you should express those comments in code as much as possible so let's take some somethings hope so or God some example code least one for each int array its enveloped list if zero equals four least one dot ad production gotta the one thing that know is most of us is what the heck is at zero Y zero zero really in what four so it will extract constants then we will introduce useless variables named is flagged this variable is defined here being used here and then never ever used again you wonder why the heck is s finke there but I want you to think very very warm about that if that starts here has some Bullen tests and then end and then enter remember it's an if member and an or or entered or or and you have to tick the parentheses you know you know the game click the parentheses here I will highlight the matching one you know that's the game we used to play when we were kids but whatever what I want you all to do starting tomorrow when you find the sniffs spending more lines you just just don't think you click in some condition I'll shift up in Eclipse control W in IntelliJ expand selection to one expression at Buland extracted variable and put a name just like this just like a good book and a good book you first know get to know Dee Dee deep artistic all the characters educators so you introduce yourself to the wise then the murderer you introduce all the all the parties and then at the crime of she said pack if that or that and that that it so respect your reader respect the bulan test and spend some more variables just to explain the code then he might go further and create objects from a god filled with the method is flag can you figure out how much code is in that method status equals equals flight semicolon that's it method with half a line of code you will do that because if you really type this code IntelliJ will highlight for and it can press alt enter and do stream map filter fine first whatever it's just one single line technically speaking in July so explanatory map explanatory matters and if you encapsulate the bull and test it plays very nicely with the filter method in-stream APR this plus the renaming and extracting is how I want you to document your code not with some comments that people will forget now it's so obvious this is - Reaper right taking the cells which are flagged if you look at that what the heck but so in the by remorse you would have put here some comments I'm sorry what I wanted to do here actually was that so don't whenever you feel remorse and you out of remorse you want to drop a comment in the code just take a walk take a shower would slap your hair yourself and try to express in code what do you think about this comment register user it appears ill submitted them some code then what should you do take below that comment hmm and extract the matter that's it simple as that why do you think about this comment loop overall engine and remove notes what do you think I am stupid obvious but what do you think about this comment hey this comment you leave there these are like and not here at if you ever been to mountains in which on some mountain trail there is a cross do you know what that cross means no log about someone fell someone died that's a comment after someone died who now we Java it if you jump on it you can learn the stream API learn it all but don't use your thoughts but don't ever have these symbols what does this mean I keep saying that though if you don't find in text everywhere this token you should find zero matches zero matches why because this is a lambda fella but some code every bunch of code deserves a name just you don't just let let your child be named dollar dollar lambda one you put the name I extract this for dots and we reference the method above the optional learn about the map or even the flat map I had that talk in last year the books here function function design patterns and I give a coding example now the point is to find this desert to ask for for review or even better work together whenever you learn something new that's something from apprenticeship patterns there's a this incredible book apprenticeship patterns which actually has a topic named learning the open meaning that when you learn something new suppose you learn Coughlin's or boosts Colorado learn it in do place half an hour and min Barris yourself in front of the others it's a deep one embarrass yourself to let the others see that you were struggling to learn something new learning the open show the pain of learning super deep key points stop if a streak will start like this bring to that whenever you stop refactor the code it will never be easier than today to clean it up refine expressive names continuously short methods or classes in Java community of three types data structures objects or logic containers realize that there are three kinds of classes three formatting and comments comments are usually a sign of a lack of expressiveness you can usually make it better by looking for names try to explain that to your doctor you have a duck rubber duck programming I will show you a picture after the break I want to google it it's an Amsterdam the green code store oh sure there is a store I was so in Perl programming is the way now this is actually from a friend of mine what can I do to make refactoring happen each day in my team I want you all to think of this I mean now will be a lot of good tools tomorrow's the day of turkey rewatch this or we go to the materials put a memento for yourself two weeks from now what and ask your team together brings what can we do to make everybody factor every day as there's a lot of things the manga team that I trained told me we lack time we let s we lack confidence we lack a direction remember the one hour time box mob refactoring session we lack the skills we don't know where the gear shifter is I need to look at the Torah meter speedometer how is it called in to no one to change gears come on after three other one year you will hear the engine right so basically learn those things now interesting very simple how the hell apply all this in my logistical now one question I have to check with you how many of you are working on code bases which are older than five years laser head that's why you're here perfect so beautiful exquisite but how do I do this and I want you to have to imagine yourselves go into a karate group karate so your first date you go what will sensei do it will put you to fight with the black belt only if he wants to wipe the floor perhaps who knows what handles sensei you have but technically normally it will put you to a fight with someone which compare it with with equal strength white belt a yellow belt right to get you started if you've never refactor code if you don't know how to extract a method how to inline a variable if you don't know where the gear shifter is what jazz do you stand I gather the logistical you slap it what do you want very good twenty bucks you are the killer I did wipe the floor with you so you need to practice and of course books drink a lot refactoring and this is an interesting book lesser-known but very good for starting the journey towards craftsmanship in clinical with seventy examples of before and after explaining the code really interesting for starting but again winco's requires after all discipline it's not the framework is not it is a mindset right it has to do with emotions listening to your emotions it has to do with the teamwork it has to do with the attitude discipline the other what do you know what this guy is doing here data recording up higher he's completely alone he's coding a stupid problem by himself haha 100 times why because he wants to practice that when you hit with the with your fists your fists needs to need to rotate to increase this this phonetic hora code to hurt more you do he does that 100 times right excite me to distract me to extract me to extract method 100 times just to feel the point look look look no rush look perfection look here well just analyzing in a safe environment look and it's not bad here there is no jira here no this isn't Radley think in a safe environment and I want you to think for themselves I wasn't when was the last time you literally looked and think of how you write code not how you fixed about how you write code right I want you to practice to start practicing maybe not every day although it's would be maybe weekly have fun now what the heck half an hour of your time invest in reflecting on how you work you know these guys two guys are facing in karate and they're hitting there they don't actually touch each other they just they are doing program practice that when you face a big guy what would you do ideally you should take a colleague and you would say to him you hold it I kick him technically this is how I initially enjoyed programming when I faced a hard difficult problem I needed psychological help when the problem of how to say squashes is too big for you you need someone there with you right - he could just be sitting like that you only have more culture these are actually doing the coding dojo oh my god four to ten people one an hour and one half hour every two weeks all these three kinds of screwed in cutters like refactoring which one of these we will do after the break DDD and then maybe some legacy this is not your legacy this is not something static exercises that you can practice there is a famous book is called coding those a handbook by Emily backe or recommended to everyone to practice a set of exercises you can practice and try to solve every problem in as many possible ways you can it's time for the break but the break should be twenty minutes okay thank you this leaves us what one in one hour and enemies for the second part right I hope I'm not missing stuff up don't omit one hour and twenty so 20 minutes break now but just to anticipate a bit because I want to reduce the the noise at the end if you want to Bob oh hi brought you stickers of course and the shortcut cheat sheet you can find editable PowerPoint that you can print yourselves if you prefer to do it in muck this is Windows unfortunately it has it is printed on two sides one in IntelliJ the other is Eclipse and if you want to have such a thing I'll invite you here while the bread so break until ten minutes past 3:00 okay if you have questions come here if you have stickers come here but please do take just one sticker because you are so many okay so break 20 minutes gone hi since you are all here while waiting right wait wait some more Fonuts you do this i someone actually are the points that were brought to me during the break so i asked about having the interfaces in front of classes in spring with being maybe required since spring 2.5 it's not but the point is i have a talk explaining very in depth about this three point this year also just to be clear when i have a gator in an entity which returns an optional of member calm they'll see all this steel member cut just to be clear the field type should not the optional off member card it's noon but i only expose it as of nullable the slides of the talk will be allotted afterwards right after and one thing interesting I've found so on Tony tip your tests moist not dry the rock not wet as the rain moist well also do pick a very good one a very good and this is the link I promise it's actually gist of some discussion discussions are around and also contains the links for the repository in which both of them submit to the code and the examples I want you to do is in case you have this big ugly method very very very very fast so we will move fast because it's never you are among the best so big ugly method you want to do here some more code what would you do imagine big big big what would you do only when you call this function here I want you to do I want it to do also these two for user story three two three what do you what do you do well one thing you could do put on if except strange request three two three and then you put it like this and then you do what it's red what do you do I'll Center thank you and it says do you want me to create the parameter yes I do and by default should it be false by any means please do now imagine here now on just you look at their upright when I will click refactor this kid's been in many places in my code I just click refractor fourth but when I call it I I come like Gandalf and I say Oh true because I am the chosen one and then guess what some others are developer will come and say and things will go out of hand they make the broken window metaphor right so what would you do well let's use the the anova mm-hmm what what extracting methods but its first let's use the most powerful tool you have in your ID do you know which one is it control or command Z we will undo we will step on our pride and we will undo the crap you just wrote good but I showed you a way to do since professionally right just change method signature move everywhere Falls and I come through but let's do it otherwise not one thing that I could do is to take this out is a method right so let's make this a method extract method named very very obviously prey excellent if you see this map this window but you don't know why you see it always press keep there is no setting yet I know in energy that allows you to do by default to hide a dialog that valid is sometimes evil it wit might extract code that you don't didn't knew about that by expanding the signature is very weird and if you don't know the code which that there are dialogue talks about just press keep trust me we have this method now what could you do well there's one thing you could do you could apply the oldest design pattern in the world duplicate accept duplicate and say very very good name three two three now this one never calls this method right so I will wipe it out but this one let's the new code in and instead of passing a bull and everywhere I will just type here elegantly I'm messing with you I won't name three two three but but the point is I can avoid adding a bull and by simply and I was now that that's the point the farm from the audience suggested when you want to avoid adding a boolean you will end up duplicating a bit of skirt but you will duplicate in this case two method calls so what you will duplicate something but just let it be a small part so I will tolerate a bit of duplication if I gain the fact that I don't couple the use cases together I have two different methods now one for my three two three one for the old or method makes so indeed holding a Buland might require duplicating stuff but reduces duplication by extracting metals but this was a trivial example should we goes for the boss level there you go yes yeah now we're talking well forget we didn't yet start with the cotta but just let's think what would you do here oh my god I want here to do something else when I call the function oh my god I can't just take this and extract doesn't work you know so then what the heck what can I do well you could first okay the problem is that this function compared to the previous one is very deeply nested so what could you do you could be eliminated the nesting meaning I will do something incredibly weird I will start with duplicating the method why do I do this because this method I want it to be both level stuff this is most level no stuff no stuff will have stuff equals to force through an unfortunate reference so if for this is Tipler indonesian sonar will company for our cut it out they'll go then this parameter becomes useless right I will just wipe out the parameter make sense now they're above this stuff this will always have stuff it means this is true if you see of course so I destroyed the if and always the body always happens but again I'd only stuff here guess what I reduce the indentation with one and we will continue doing that the it remains some code there agree but let's continue let's say this is the first one is stuff floss accept this truth and just just what pieces can be deleted if you have IntelliJ configured good not like I do it will be grayed out and you can just it will South - you shouldn't I believe the field printed in Arad what's what teachable statement oh good delete Oh did great delete that this is how Bianca does it my usual pair she likes the mouse problem now this is no fluff I know this is so this is stuff fluff this is wait a second this is stuff and what if I have no fluff no fluff means this becomes false false means this disappears completely staff knows laughs next time let's try to do the next one if starts different them moon then if you enter this method abandon all hope meaning that you have to stick in your mind the fact that and else will follow what could you do otherwise you could sleep the if let me show alt enter what's wrong invert brilliant now guess what we can really can reduce the number that the depth of the method by just deleting the else here make sense there you go and then gives what would could happen next now the reader how does it read this instance equal no return -1 perfect like see the seat answer then a system up in the now it will come something you weird for you perhaps when I will duplicate this and I will say 3 2 3 I will allow the four to be duplicated but we will get to that more in that perhaps if we have time when we do the actual coding data I just wanted to sketch the de work the work with gülen's and let's get to the famous a little variation it's a variation of the of the famous video store I didn't found any other for all of you out there coaches or extreme programming coaches or agile coaches if you wanted or teams something interesting do this cut is amazingly laughs now it's a bit of variation because you see when this is the refactoring kappa meaning that surprisingly we have a unitest map like production and this unit test will hold you you are safe whatever you do this test will guarantee you don't mess up things and the fact the car did did they at the point of the refactoring type of kata is that you will blindly trust the test the test is supposed to cover everything so it will cut out fear it will just it will destroy fear from your heart so we have movie and customer let's start we have two classes one is small one is big if you were in production you will not hesitate to tackle the big one but if you would be practicing karate how would you have there's a karate lesson start what this warming up so in a normal practicing mindset you will start with the simple one very easy shift of 6:56 control at a more the equipment these are the two apples you always have in hand you don't think it's what a hundred is great out if it's great heart and target yeah thank you bye bye get title get price good enough in picture with a big eye and trust me here on the big guy it's not love the first thing you could think doesn't even knew is noon a valid option for this field well you could look around and you might see that at this line if it's not known and something honestly speaking eyes could not in normal production you will look some more but if I'm practicing the point is try to put it in what would this mean that no one will be ever allowed to pass me a moon which is good just out of a twist I can remove no completely don't just propagate the null we watch your caring dude don't crash at the first point you see it right pretty good there Wow this doesn't make any sense good let's try this it should use delete okay hey look Wow well then let's analyze the big one because the move its kind of be done I don't see much more to do here it's do I do have wait wait wait let's use the 51 finally so one the remember zero point zero point one percent raise a customer customer when I start with this one what what about yet it's a map you can change that the type what is the type like type it I don't get it with yeah in the movie what's here it was what's this what should I do here Oh perfect how do you check that well you will see first of all what is being used and then you will see maybe this children's Oh this jet has a parameter to the constructor whoa this constructor takes an int whoa the children in rebellion eerily Caesar is an INT wait a second if this is so what will what will stop some evil developer adjustable but to stop them stop them of course you will build an enum you could put it next to this class or you can burn it in you know yes I do movie type this is a bit of redundant right I'll just name it type and I will put children regular and a new release in normal production you will check if these numbers are useful or not if there are you'll pass them like that in my case soon they're not useful so now basically at this point I should commit really Jesus even development and I would say oh my god what next whoa whoa whoa ready let's see well this should now I will do a big bang because it's hard otherwise we will we will just kill this we will click on this right migration didn't knew that type receptor oh look everywhere now good now any customer test what happens here this is movie god oh how do they good and then movie children I'm really in children is see ah ah ah really really mmm let's get there first this is a thing that takes sugar in Java if you switch on an enum you don't ever need to repeat the name the name of the enum is synthetic security it saves you path but good and then we run the tests you are good okay well good good it was a big bang right quite quite nasty enum I wanted to do as much commit as I can but just before that I will just need to hack this and then let's get with the big one whoa what's this customer of a name this name suggests it can be found of course actually if you take the name through the constructor doesn't have much of the sense to hack it up later the scepter but in this case there was no setters are good for me whoa gee this is good a if D less than zero is d then put in the rentals map for movie and an integer whoa and this is the rentals oh my god rent rent movie and rent okay what happens next somewhere below whoa whoa whoa what's this eatery old school now first thing you want to do is to make space visually to group together if you what would you do with an iterator iterate excellent bring up stuff and you look down and you see the oh wait wait wait shadowing avoid whoa who would do that but first we will do we will use the most dangerous tree Factory in the world moving lines I will move lines I will group the lines together and as soon as I do that in the energy wakes up and say wait a second what are you doing should I replace it for each please do brilliant excellent snap each equals movie now in line movie now if you didn't you in line what would you do here you would forget where you are heading right Google Maps will tell you head south so then I will in line this simple as that it took me half a half a second then this dot watch it that is a mind this is narcissism folks no more this what this what is no no I don't know ok so then then then then result in the inventors key said movie movie did some undetermined amount I know what you're thinking kill it kill it I won't yep I first want to Rena what happens here I am iterating over the rentals and then guess what I'm doing I'm getting them the deal what did that bones for a two day new release rental with the second 2d one it's a new release in it and which you see the parentheses look look look yes I click the font this is it and what do you do here you do an extract what variable and you press here you do expand selection and there you go be perfectly this nearly nearly nice good now if it's a new release good and it's for two days ring for a two day rental they what is three days rented there you go what do you do next exactly so don't ever hesitate and local variable which is which has a good name can save lives folks and rendered good and now there is rented way better and then guess what larger than one which it was too yeah but the hundred is an integer ah now comes the fun part let's do exactly why this is a super interesting part the demand even guy folks will be very happy I am aligning the code in the specification the two in the spec appears in the freakin code why is there because they will change it tomorrow two changes from two days to three days you will search for to write net Hulme tomorrow now if we extract the if be announced course put a bracket because who knows who comes from Python and then let's see next a string value are what the heck is this okay one line good head fertilized hmm but this first gear is too too much clutter let's first take out the switch shall we extract method yes it says it requires the movie the dis amount and they is rented you have to better than that the thing what the heck happens here I am adding I'm putting in a map some values and then I'm getting them back but I'm inserting over key set and I'm getting it from the map you see what I mean you should have sonar configured in a certain way it will tell you you could iterate over entry set the code new looks worse if the depth but the point is if you are putting in a map and then you are taking all entries out what are you doing you are avoiding to create types you just didn't there to create the class named random why look rental has to field really that's amazing many maps and I had engine production many times I was using maps because it was very easy map now I was having map of something to map or sizing right think JSON and privates final was that is a movie movie so I have to have got to create design right in there there is rented and then you do this and then everything that gets born on this world gets born in private so I won't make set up a get off yep unless they are really necessary instead of a map of a movie - integer I will have a list of rental good which is a good really good and then instead of doing here put I will do add of what of a new release random renter of MMD and if deemed less than 0 - ah perfect this is a perfect thing to bury here with the pretty more reasonable name of course vinegar and then you have a rentals and new iterate over rentals and if you iterate over a rental it will give you a rent sorry for the wrong option happens all the time change to be rental and then without much of a thinking shift f6 will tell you should be named rental please thank you okay now most of these things work as well in both now in this place working energy and eclipse this is where I need to get the day's rented out so get different go and get this and then get price code watch this this is actually the movie uh-huh rental can get I ever have a ghetto okay get moving ok and ok and get France code this is from the jet movie ok ok ok and yet this is way out of your requirement and it it's your butt and then let's move on what if I could force the dish to become a renter whoa to have the guts to do that it would mean that that other guy could give me directly honestly honestly say I've never tried this but I will try this live with you but what if I do adhere the rental in the rentals rentals dot and what if I call here ad if I inline the ad rental will it no good sure ed rent we didn't know - yeah let's see in life whoo good job everywhere nice so just knowing honestly I never did that before just knowing the way to go you can't drive with your IB t-t-t-t-t-t-t-t-t-t just knowing how to start you can grab it go half the way without enter the seniors of you know what I mean okay and then name equals name equals perfect perfect I eat rate over rentals I determine the amounts for each line so let's steal the switch let's move it out mean days rent in one thing very easy to understand this switch if I extract the blue block the blue block needs to get in the this amount needs that there is rented and also needs the rental from above in the blue block I need three things from the context for which I extract imagine three strings three what can I do one thing I could do since I am passing in the blue block the rental anyway is to let the blue block this one cool the gate there isn't of itself make sense so I will inline this I will try to extract again only two parameters see which is good I will name it but before I go any further I look let's start it yet sometimes you do that because you don't see all the implications you tentatively but we saw you do that you commit let's try let's click ok now extract this and you will see that this amount is given as a pirate and it gives what what value is always here zero so undo step on your pride let's try again what if I take this with me no only one parameter nice so you see by by playing with the selection block I can control how many parameters I will take get this amount yep gee let me know Mario Fusco yeah when you cannot find a good name for a thing choose a wish one so and then let's see this amount equals this amount really decent man what is some what is this amount this amount is added to total amount obviously and but amount is your amount or kill you oh can you old potatoes can you owe a liver liver you Oh Jake Jake well exact your money so then this is actually what price ah at that moment you will aggressively say total price but you won't stop there you will do control shift s and you will search where are you this just don't tell me this price this price again where are you papa aha again this is the string I can't change this but we're fine this is why it will name the mount right because in the specification they copy paste they didn't think it was the amount they put the amount here and there ago refusing your powers at that moment I will I will very happily destroy I made the comment redundant this is at which point I need to come back because I want to commit I will name it how they fix all the intelligence or walk on the street like this you can recognize them the Eclipse ones don't I'll shoot at IntelliJ what the heck the option whose compute price just not yet place bake price make price generalize computer click chocolate five six five options before you name something directly spice good and then price equals calculate buy some rental obvious and then add free content report frequent entry points plus plus sometimes laughs plus again Rick you come with me when we say add the frequent renter point now you see how much I spent typing this name and when I looked at what I got back I'm disappointed I typed twenty characters to suggest to find out that I did a mess what the heck is this have you seen Forrest Gump when he played in pound or the word the wall this time let's take another path what could you do look inside and you see it does plus plus sometimes plus plus again this is another simple move but you can think of this you can make this method not change something it gets technically did we change the integer it returned one back but it feels like it changed the audit came in you can make it more pure forgive me the not exactly the concept but by returning what the Delta what needs to be added to the other value meaning good and then if I do that I don't need to take this but this will be coming in and this would be 0 here and this since this is how it grayed out there you go it disappeared from here perfect and then add frequent hundred points oh let's run the test eek I ran the place because I heard you oops and then you click here in great despair just to see that you messed up the freaking rental points and you you are right - got to put here one you run again it feels warm isn't it the test keep you safe we can't imagine how it feels when you are we were released of the sphere and then this is obvious said honestly this add anymore doesn't add anything did what come here a no-compete big no make infer calculate consistency very well calculate of course don't invent seven words to kill the poor reader calculate perfect good you notice how I obsessively compulsively run the tests and then add bonus for a 2-day energy center really redundant you see you make the clear explicit itself there you go perfect okay it goes well let's go over this notice girls go further on a string value of really and then plus and then shows figures of course you are all looking at this now I will there to see something that I might be sued for but let's place our tent this is not a torture should I do this how does this look is it better oh my god oh my god oh my god let's hover them off now this is the famous interview question the only thing the poor fellow understood failure on the table performance is to ask us about string builder and string concatenation in a for loop I would dare to say something bored now see this is this is this is stupid string builder only only makes sense in case you have mother no more than 101 1000 elements in the list how many movies do you think a customer can rent at once iterating over that technically speaking technically speaking Java might not even loop can unroll the looping can destroy the floor and avoid jumps to take advantage of the of the look-ahead execution in CPUs but basically the four might not exist in assembly code if it's weird it is so so it's only place for more than 1,000 elements but I want you to ask your interviewer my friend we're concatenating 1,000 things in memory do you like working with 50 megabytes string can you imagine the face of your garbage collector when it sees an object contiguous of 60 million octaves bytes so what you should do with your interviewer is is this how you write code in this company I sing in a 50 megabyte string are you sure you want to do that if it says yes you live The Art of Living the interview the art of counter-attacking what you should propose him as in a writer and slash the content once you write that only this on the socket on the day that we own the club whatever search it out of memory don't just pile up stuff so this my friend I'm sorry but disable inspection I know there are places in which this space but nothing my mmm oh yeah okay good good good result equal Oh Gnosticism great I miss that hey good hey the gathered is no longer used bone digitate perfect name record name can ou add filter life perfect perfect this is easy extracts it call it it is named add footer lines excellent and then you wonder what the heck am i doing not again so what would you do here ah I don't write and then you do fix the stuff meaning that result because and then it's a fling and then I want to just to touch on something the habit that I saw in some teams to put final here and always the meter is something that you are very attached to because has no has no real value it is so superficial that many people are very very very very passionate about this topic should a bit final here or not I want you to see to see something why would why would you put final there to avoid what the method changing is that value right let's undo a bit to show at me whenever you change the parameter the parameter gets highlighted so what if I never do that what if I let's force a bit forward return this and then I will just concatenate with this and then that's it in this case no it's no longer highlighted why because you never assign it again the IntelliJ do that for you so why cluttering your code with final I know very there are people very very enthusiastic well how'd you feel like but personally if I do that I will say plus equals and never run the test in which case this is not add what this is how okay I wouldn't do calculate come on man but calculate futa Rini no come on words created to V generate seems like you're playing the format is a nice word or the kind of too much or right or creates vague mm-hmm generate another format this time our goat format okay format and guess what this comment is redundant kill it good format foot in which we have a footer we should have a header format header I will extract half a line you might be laughing what the heck is he doing yes haha line why because I want to have this method on a certain level of abstraction at which place the only color in this method which annoys me is this do you feel this line doesn't belong there is the only line still working with strings have you ever washed your socks by hand should have what is once near like you don't wash your socks in your living room let me let me defense earn Wow I don't have this is the problem it's cultural district what the examples I give you this point doesn't belong there it's too low level okay algorithms for determining the price of during Black Friday right next to try with resources file open file are you exception read line flash new doesn't belong one is very high never one is very low level this is on a different level of abstraction so what should I do extract method you're a quick learner and I will name it so figures not format rental body but it's not yeah Budda Budda head but it's a body line you take all the possible opportunities to explain as much as possible but you see we learn the code as we refactor we don't just study the code now we are there in the cloud digging our way learning and refactoring this is how you should work right okay okay so signals for this rental in with great satisfaction delete their ego format body line perfect perfect no one noticed the name of the method statement what at which that this time is obvious right it is format people obvious formal form of the feminine format statement oh we still have a lot to do however this kind of wraps up the the reflex behavior the wrist the reflexes we've we didn't think that much until now for the past fifteen minutes but now comes the fun part there are two possible ways let me here just look at this method what does it do it picks a random and it starts asking the rental rental give me the movie rental give me the different rental enter enter enter it yeah just leave me alone regret in what class is this for in this customer customer Java does it have anything to do with the customer no it has to do with the state in the rental right what should I do move it to the Randalls is called this feature anyway very important to put names to the code smells you face if you ever saw any movie with some bad spirits you'd have to put the name fight them all school the point is aren't to move this matters in the rental move do you like to move it in the rental at this point I was first terrified I just read this first honestly I'm a bit sorry for the move with again f6 when I saw this dialogue first I got scared no no no that why would you know to move in the rent or who what the hell how could the idea this worsen intervening but if so how would it know to move it a rental exactly exactly but then output public here and a refractor and guess what it moved it in the mood right it's the point it is in D yeah I was it's a new class ah that's right okay let's make it public right and then it read i'll tender move to rental java go and then you have the rental next to more respectfully and then calculate rental now there is a sign of narcissism here and there but we will fix this in a second next thing can you spot another another place in which feature envy happens when you will movie rental gimme gimme gimme gimme gimme so whoa f6 out because has nothing to do with the customer calculate price is just now in rental yes what this is in line but if I do in line all these places are my fingers will hurt right so what what I do I will do it again the hipster stuff look ma I have multiple car I have multiple cursors if you master this one you can follow no it took me 2 seconds right and then back to the Castro would you tell me or another example of feature energy here but wait I want to just to ask you something else first that's a tricky one then I delete anyone any other line from this block of code well Sunstein's told me yes we can we can do this never do that just to be sure never never but one other line can you delete we will do that we will do it it's perfect but no wait wait with me now the team's normally try to delete this line doesn't work but very dislike doesn't work and they just end up focusing on this or this only line a ticket what we didn't mean to inline this variable it will mean to copy this expression you both here and here what would be the problem three problems what could be the problem in this case first of all and what is the problem if the computation is very expensive it will hurt us but form of second problem second problem we will see about it don't leave this function returns different values it won't work right thus if the function is not idempotent that weird word right on the side in advance it is not idempotent it won't possibly be correct and the third one the most horrible one if the function inserts a line in the database that's the end my friend call it twice to have two lines but now it does just what a switch okay now can any of you approximate how much computer's CPU cycles does it take to execute this method there is but most the fingers of a hand has to do it completely compiler optimizations look ahead execution hard stuff hardcore stuff but in speaking this you can possibly measure the exact yueshen time of this it's 100 times less than invoking current families so what would you do in this case because it is very fast idempotent and no side effects you could inline the price in arrogance we can see the power of having pure functions you repeat the call there you go at which moment you would look at this line and it will figure wait can I simplify this a bit I will tell you how I will extract again it sounds very weird I will limit again format but I will be glad there is only one parameter that I need I will name it format body line extract it go there and guess what kill this one you see how you juggle with refactorings if you know what you can do with your tool just by sales moves I never backspace backspace backspace backspace control ixnay ver I'm just working with the ID I put in half there was actually they were if you look very carefully at the replay there were three places in the entire kata in which I went off the IDE in the rest of the of the of the moves are are almost entirely safe the tests are almost redundant although Dakota Bank right good now in this method it's hard to remove but we will in a second but first I want to ask you do you see any other example of feature Envy in this class this one so they move this here on top what is the oldest design pattern known in history three letters so on I'm kidding now there are very weird patterns what is an abbreviation but it starts with M MVC Model View controller what the heck of a concern is this view view interview doesn't have anything to do any holy entities so then what do you do you don't push it there actually you realize what the heck am I whoa we are in the entity why would I imagine an entity at which for a moment you would like to extract this out we will take this out this out this out but you will also take the general with you your work is satyr oh yeah okay we will move this unfortunately to my knowledge there is no extract class that why look how I do it I will make a public static class statement formatter do to be chose today nested class here right in front of my eyes now this sounds a bit silly but in realize is super useful because in normal life you are probably extracting from a service of some sort in which cases when this goes red what we will normally do you will duplicate the line and move it inside let me give you a hint what this is very useful to break up classes in service classes in spring but then look technically speaking I want to fix this to compile and then I will move it up because I know by pressing f6 you to remove to another file in one second so I will search it like this renter's where the do renters come from where did well do rentals them fun teams which are very eager of doing opie when they look at the art enter menu they choose field if you make it a field you burden the class now this is very addition it's too short an teaspoon of this but in essence the fields are things which are shared by many methods you don't by default water field what should I choose here very well it wasn't there yet but I will make it a list of Renton and then rentals there you go I am for his first default put something in a Metin don't trash it to the field but put it in a parameter to the method this doesn't compile what's this name well since this class will get extracted it's the name of who is the customer we lost the context in which we were we need to be more explicit customer name needs to be a parameter which needs to be passed here they're driven developer my friends there you go and then I didn't I don't like the order control it's six Siri ordered in ago what format statement this one now it combines by the way if you notice the the the package name that I prepared for you today I won't do that much but for you explicitly it's a horror packages so the statements for a matter good good good good good good good good good perfect and then here and then we end up in the customer because we need to run the tests and the tests will crash of course did know is that just moved this is a 503 followed the very did they say that moved to the trio to ominous yeah it moved permanently moved so then new statement and then I need to pass in what now clinically speaking if you look carefully here you might suggest me that instead of testing these two I should pass the whole customer now if it were only one parameter I think none of you would have suggested that if it were one more I think all of you would suggest that that business is incredibly brilliant kappa for brilliant to do such a kata to forge it next month it really resisted to parameters if it has two parameters you're exactly on the edge you can choose one or the other now just for fun let's see where it takes us if we choose to we use primitives not big objects let's see what happens in the test in the test I need to get from somewhere the customer name by the way I must say well I must say I love this not not not not just a minute wait it is a costumer dot getname we didn't had one mmm g10 and then yet by the way one once more to save your shift with scroll can you feel the joy shift with scroll good name and we need to do what to do custom about yet rentals well I don't have to get the digitalis mediator the Atlantis ago and then down here I need I need to put in here yet friend good run its you normally you should have written the commit message on a paper on a paper before you started any of these steps you'll but now you I have to ask you what value is here always so know in your be test should be a bit generate as possible I'll have tomorrow another talk with the art of interesting if you have to do then much here wait wait did I went wait what am i doing I'm building a customer I'm sticking grantors in and then I'm taking them out and given to do you sit what should I do here I love @j it's just beautiful I want to make this ladies and here I say at least there is no such thing I'm not yet in Java more once more control space 2 times yes do you see home as much at least control space most mu canto spins twice haha this says now folks these things allow you to focus on your goal rather the gearshifter very important make very part of this printers obviously all the time presses here and then rentals you go good this is great out whoa really what's wrong with my remove think we play afterwards and then of course this needs to be static imports don't static imports nothing else but geeky stuff don't import your own functions and then guess what this is no longer a customer test good this is actually a treatment for matter now I always do that and ok and then run again statement I lost I did a typo when it's gray blue red yellow would you like to rename it to statements formatter IntelliJ knows best their English than most of you I want you to be fertile - B - B - B - B - waking up well the sorrows it's a dissolve not fertile words Azores this one this one diction that is my friends dictionaries so spend five minutes per week in the dictionary I think Kevin Henley had this one word per week it is very interesting at which moment you realize that and that and that and then we have more and this is more than five lines can we make it shorter technically speaking these variables in which I stuck stuff in a 4-iron classically named accumulators accumulators are variables in which you add stuff and they are bad because they are highlighted or under Lanvin you see what could we do if we had the guts to do it knowing that at least aren't more than 450 elements what could we do and first of all I want you to some people suggest I do you want me to extract this let's try quick there are multiple output values for the surgery you are touching this this and this should I take out the bazooka for you of course we'd ever saw yes great well what new and then folks invoke this is a new class in which all the body went which has three fields more that I can ask her words take out of it the extract method object is buried in more depth in the other previous episode it's a two years ago go and look and on this one Devil's pond so with my eyes very humble I do undo and then I think what can't I extract the body of the floor because it does many things a violation of the single responsibility principle arguably one of the most important principle in programming so what could we do if we have the graph using reason not me oh my god then what in the first for leave only the first line in the second floor leave the second in the third floor leave the third then I will localize the very you say no no no I did something wrong yes now that's a more programming focus that's more programming really I can't possibly do any mistake again huh for this reason is valid under the path are we good now we look think I will localize the variable next to this for I will localize this variable next to the four I will localize next to deform at which point indicate she will how can we fix this now first of all even without Java it without thinking we could just extract this to a method and further on break it into little methods but with Java it look what what you can do just go here in the art enter zoom [Laughter] so what the heck is he doing what the heck is he doing soon window here for too old too stupid for soon my god numeric stream some rank up much more clips and then result result result there you go maybe guess what this could be a mess up let's try that let's make it a method in one single line named first calculate total ran third point we go this one control W explain selection or even battle just click anywhere control at M will ask you how Marco do you want to extract very nice and then calculate total price there you go and thank you slap these two can be inlined down there look now I will jog a little bit man in line in line yeah extract again for mod for third and then guess what in line this one happens nobody knows Fermat's water just give the rental because it can compute itself by calling the two new methods there you go in which case I can also take this out actually this is a bit more concrete doesn't work without enter just the side effect for you if just click randomly in your code the IntelliJ and press alt enter in various places see what happens just for finally I had people carrying back have to trade well know how I discovered I didn't do something with that plus equals how is it called rentals thought stream dot map control space format body line dot collect door joining thank you and then of course I don't like that I want to be good and then this becomes redundant and with fear in our souls we run the test you're good you're good but and then but you realize I'm kidding a bit do you realize how how good it feels not to be able to in which case I will be now father said something like the benefits of indirection because some people think why would you expect hustle like I mean thing but look what I extract by a wiser name format but how about that which can lead us join declaration with assignment to perform like this this is this is kindergarten and it can be drunk cigarette for a header format button from on Twitter what mike posner what house now how did it all started repeat before now you know what happened in my many many many developers aside a word for programming by teachers we who learned three thirty years ago those pictures thought might remind people that for is an experience of operation it was nowadays what you see isn't what you get you don't know right ask Ricky once you compile is not what gets run on your processor thus or doesn't happen right if you're thinking about performance this is 100 times slower than a bear for because you tied the hands of Java isn't allowed to do much of the musicians in that case where the code is way more readable it's another story I don't want to go into all the places but to keep focus look how things can and can can turn out if you have the guts to duplicate a bit yes Joffre live how many of you are in production on Java 11 one quarter one less 20% yes you can but very good that you brought it up so you can use to repeat to remain on using the T operator in java 11 you can do stuff at once with but further on we still I mean you probably all of you are just like you know like in Gladiator this is great great great kill great kill guess what if I go to the rental I will figure out something no nothing interesting we get days rent reason isn't needed anymore because all the logic on this field is performed inside the object so I will be litigator this equivalence to plus ten points increase to delete the gate but oh my god woohoo now in normal applications is hard very rarely do that honestly but we were all thinking about this switch right it all we all dream about switch we go home we have a beer and we dream about the switch the switch is the switch let's see how can we how can we fight this which let's clean up a bit first of all I this for adding and then anti-pattern and then it no the switches and if used like this is very weird because they cannot localize it like this here I saw teens doing this about one other trick IntelliJ just select the block of text and just press a parenthesis it will surround it with C but you know why they do that because they want to have the double variable defined in both places and look what they do because otherwise it would share the variable yes you see that the misery the variable think the scope is a mess is that whoa this is no no never so what else I will tell you now the rules of a hygienic switch is among once which has three rules by fashion you never read it it's a first rule it has a default its I attend that you certainly for thank you and we normally put here what you know the Pandora Box on the bottom small with her wings it was the hope I mean just so you hope this how can you put a candle this is the first or the second rule of the switch the switch should be the only instruction in a function the only instruction in a function it's not the case here you see now because there is the turn here and there is the definition of the variable oh my god how can I make it the only instruction in the function I will and that's that path no matter how weird that is I will do anything just to avoid having any other instruction besides the switch not you boy and then here now I want all format for so rule number two matched ah doesn't compile fatigue the doesn't compile because I forgot to do here return so second rule of which either the switch is the first and last instruction in a method for another in other words they're the same with Joe from Venkat in functional programming we don't have brakes but and then this is the only instruction if you collapse it it's the only one good and the third rule of switch the best one every case must have at most one line exactly one line which is weird what do I mean what what do you think I mean extract you don't think do things I want you to remember extract extract you name it how come on queue late a regular price cheap and then you can you can unwrap this and look it's online grid next time I will extract half four lines just out of consistency I will name it computer outlet or new leaf keep it on oiz me that by log really annoys me and then calculate children now just to be sure just to be fair that when you see that that dialogue and you don't expect the code that it says it looks like this bus escape going looking at that code in figure out if that code has the same business meaning as the code that you extract now if it does maybe think of reusing the method that you extract but don't just do accept blindly because you might end up tapping together pieces of code which doesn't have anything to do just happen to look sync tactically the same and look what a nice switch I have been Java 13 hope it will lucky Java okay good now this is the first of the three rules of switch the only instruction one line per now you might wonder if this method returns void what would you do I would call a method and then what this unit break okay if you saw Matt it it gets lost I know I know but the point is the same or every case just calls one method on door there's a one-liner operation good now from here on there are multiple possible paths actually about relevant paths many possible and I invite I will commit here lay from here I write you from here on please exercise all the options that are you will see on a horrible slide that I will put for you just to name a few and I will do harder mean I will be rough on me because you're tough guys for first or map the first polymorphism I will just put one word in you will figure out the rest which is a path that about takes in his recording this is one way I'm not very big a very big fan of extends to be honest second possible path just sketching just sketching just sketching movie yep but wait a second nobody notice this is pipe that's good what do you do you stop you on do until green you clean everything you go there in is the shift f6 and you say act thank you they want to do together yes please the enter the parameter yes please and then you go again you say movie you don't just let them there you see to fix it so easy if you know the and then you say on the type on the enum yes compute you need to pass today is rented here but this could allow you to do the following weird stuff just sketching I won't finish anything you have imagination you do it yourself I know this is super weird and if you look at this is sitting on this at which moment you might do abstract method in an enum many people consider this word outrageous but the point is you can have implementations in the enums long live java but I think English speaking is also in Scala distinct stuffs below as long as you don't put here 100 220 lines of business logic and as long as you don't need any dependency from spring Auto wired won't work yes I object I object very good aha perfect perfect perfect - - this picture I want to repeat the argument was that if you do this switch multiple times so I will also have to fix this right now I'm I'm a bit of unread you got the point I will undo I promised I won't go much in detail with this I will undo back use an imagination from here on maybe even practice it here I'll leave it like this and now that is the question did I the point was it's wrong because what if you do this which three times to compute the price to compute the max allowed ran rental days habberman and calculate that price but calculate our enter point if you switch on the type in three switches three magic number P you stop and maybe you think more careful about polymorphism because it's better to distribute them in well-behaved objects perhaps especially here will the particular logic here and here use specific fields of that subtype that's a clear sign if the subtypes have different attributes it's a clear sign that you need inheritance there despite GPA so indeed if you do this often I mean I wouldn't go further and say here public int public abstract in M and then guess what n - don't do that if it grows it's it's it's it's exactly it's very ugly I mean what I imagine the switch with feel with business logic what the heck what this no no stop this indeed good point this is one is this one what you meant right then another option you might have a little bit more conceived is the following the famous fight between the switch and a map map I want I all only switch sketch function between E integer and the double the signature of this method because if you look carefully it requires the is rented what you will lose if you do that if you do this what you will lose I'm sketching and film and not I'm depth if you do this if you do this you will lose this if you do get and the key is not there no pointer furthermore if you stick with the switch like this following these rules in Java 13 plus when you will do return if you missed if you switch on an enum imagine that you switch on an enum you skip the default at compilation time Java will check that you covered all the case of all the inner values it's amazing if you add one more value to the enum the compilation we will fail in the switch why because the switch has to return the value if it didn't sell in any case what's very should it return right so the thing one compiled which is amazing there were 13 plus 14 which keeps postponing it so this is what she lose if you go for map another option controls Russia bit is to put here a buy function last the talk I give here last year go and watch that one one of the other things I saw strategy pattern interface price calculator in which you put that this lazy as I write that in the implemented between three places they are already there they all will get you somewhere but you get the point the rest I will leave you to play ok how just leave the code like this and I would like to make some some sorry some retrospective history and what did we learn which which should be the topic of the retrospective first of all what do you think these guys are doing no nothing pay attention opting assembling an ak-47 with their blindfolded my father used to do that when he was in army with the lights out they put them to assemble their weapon why because in case the enemy strikes so my point is rename an extract method you should always know how to do that always know how to do that listen so now we play the bit with the scope of the extraction when we included the variable inside the block we needed to take less parameter in the block that we extracted remember then we didn't do the other kind but if you take an expression out in a variable before the method when you extract the blue the bark blue block it will take an additional parameter and then guess what in other places you can give other parameters you that moves you can learn with time very very powerful move which will envy we saw means that if you call a method on alpha passing alpha you can always move the matter of in alpha if it makes sense the bidder Oh Oh P and the most powerful forces in the universe the third one is control Z the second one shift a fixed na that's a tool the power referred and I cry with you together I reverted in total weeks worth of work my whole life but the first the first the first the first power in the universe I ruined the surprise it's love love for your reader care for those who will read the poor souls who will read your code in speaking about love do you love switches to do her to do hygienic switch her only instruction in the method the default throw new Jesus driven development and then one liner per couple keys right then if you do that you recover the love okay so Africa I'm exactly three seconds past the time that was all soaks if you have any question are you more than the like to have to discuss here I'll be here today all day and tomorrow also until noon otherwise I'll post the slides here this is a get location or when I push all the code already is there we have these you have in then just just just just this just ask me stuff okay thank you very much folks and have a great a Vox [Applause] [Music]
Info
Channel: Devoxx
Views: 38,083
Rating: 4.9316688 out of 5
Keywords: DevoxxBE, DevoxxBE19
Id: wY_CUkU1zfw
Channel Id: undefined
Length: 162min 20sec (9740 seconds)
Published: Tue Nov 05 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.