Testing and Refactoring Legacy Code

Поделиться
HTML-код
  • Опубликовано: 3 авг 2024
  • In this video, we take a piece of crappy Java code with no tests. Our objective is to write tests for it and then refactor to make it better. The code has the most common problems that much larger legacy applications have, like Singletons, static calls and feature envy. It also has some design problems. Fixing that is quite hard, mainly when we need to write all the tests before we start the refactoring. Another rule: We cannot change production code if it is not covered by tests but quite often we need to change the production code in order to be able to test it. How to solve this problem? Well, I hope I can answer all these questions in this video.
    If you want to know more about Software Craftsmanship, please check my book: leanpub.com/socra
    If you want to play with the code, clone it from: github.com/sandromancuso/trip...
  • НаукаНаука

Комментарии • 145

  • @detaaditya6237
    @detaaditya6237 3 года назад +16

    The way it is explained in incremental, step-by-step order makes this video feels smooth, easy to follow, and beginner friendly. Even though it's already 8 years old, it's one of the best refactoring video around. Thanks a lot sir!

    • @ChrisAthanas
      @ChrisAthanas Год назад +2

      I wish someone showed me this 9 years ago, I would have kept so many legacy code revamp jobs that I attempted and failed

  • @IsraelSantiagoBH
    @IsraelSantiagoBH 3 года назад +3

    Definitely one of the best videos about testing and refactoring, thank you !

  • @dawid_dahl
    @dawid_dahl 2 года назад +3

    Finally a testing video showing ACTUAL testing in real life. Instead of in a pristine, best-case-scenario where there are zero dependencies and oooh ‘magically the only thing that needs to be tested are pure functions’.

    • @ChrisAthanas
      @ChrisAthanas Год назад

      Yes
      This kind of thing is still not taught in any school

  • @vuluongtrieu2609
    @vuluongtrieu2609 5 лет назад +9

    0:50 Legacy code rule
    1:18 Tips:
    - Start testing from shortest branch to deepest branch.
    - Start refactoring from deepest branch to shortest branch.
    6:02: Tip for testing with outside (cannot mock) dependence.
    57:20 Refactoring static method.

  • @janicknorman9396
    @janicknorman9396 5 лет назад +3

    Definitely one of the best videos about testing and refactoring.

  • @RiteshPuj
    @RiteshPuj 5 лет назад +2

    What one learns from this video is what beginners learn after years of experience. Thank you very much for this video... Truely grateful.

  • @webbsk
    @webbsk 10 лет назад +20

    This is a really fantastic video. I am C# developer and I was still able to follow along perfectly and learn SO much. Thanks so much for sharing this, you should be proud!

    • @eNSWE
      @eNSWE 8 лет назад +2

      +Samantha Webb agreed, this helped me so much in getting a feel for how to deal with legacy code and how to design my tests in general!

    • @esjihn
      @esjihn 5 лет назад

      I also agree. I got a SDE job and my first project is refactoring a massive program with hundreds of classes partially in C# and VB. I had no idea where to start and there are code smells all over. This video has helped me tremendously by applying the same concepts

  • @HansLowell
    @HansLowell 9 месяцев назад +1

    Feel like ive been searching for this since forever

  • @professorclever6487
    @professorclever6487 3 года назад +3

    There's not many refactoring tutorials as good as this! Thx.

  • @kauanmocelin
    @kauanmocelin Год назад

    Very good insights on how to deal with coupled legacy code, thanks man! 👏👏

  • @wolframschroers
    @wolframschroers 3 года назад +2

    Thank you very much for this in-depth refactoring work! Very specific and practical examples with general and powerful techniques. I certainly learned a lot seeing you in action!

  • @SandroMancuso
    @SandroMancuso  11 лет назад +4

    Absolutely. Both PowerMock and JMockit are an alternative. I, personally, discourage the use of both since they allow us to test things we should not be testing, like private methods and other internals. Since they make testing legacy code easier, developers are less inclined to see the issues the code really has. Some people use the expression "listening to our tests". Basically, what it means is if it is too difficult to test, almost always is because the code is not good enough.

  • @prashant2782
    @prashant2782 10 лет назад +3

    Very good video on writing clean code with refactoring. I liked the various shortcuts you used as well. Almost a putting it all together kind. Thanks for sharing!

  • @pterovich
    @pterovich 8 лет назад +1

    Finally took the time to do this kata. It's great to see your pov afterwards. Thanks for sharing!

  • @ahvetm
    @ahvetm 11 лет назад +1

    I really enjoyed this video. Your experience shines through in even the smallest change you perform - even choosing the most expressive method names is an art that takes a long time to perfect. This is going to help me a lot at my current task at work! My favourite bit was your test naming convention.

  • @zedisdeadz
    @zedisdeadz 6 лет назад +15

    6:55 the seam technique is so good. actually solves 90% of the legacy code testability issues :D

    • @PuerinTheHunter
      @PuerinTheHunter 4 года назад

      Thanks for calling attention to that one! It is really useful.

    • @PlerbyMcFlerb
      @PlerbyMcFlerb 4 года назад +1

      Feels a bit like test induced damage to introduce inheritance like that, but you can always refactor it later, I guess

    • @refactorear
      @refactorear 3 года назад +1

      @@PlerbyMcFlerb It's called "Extract And Override" and is one of the techniques to create a seam taught by Michael Feathers in his book "Working Effectively with Legacy Code". If you were starting from scratch you'd likely be using dependency injection but since that's breaking the public interface and having to update lots of classes just to make it compile again, it's way better to use E&O.

    • @alexsmart2612
      @alexsmart2612 2 года назад

      Except now you are not testing how your class is interacting with its collaborators at all. You could replace getLoggedInUser() implementation with throw Unimplemented() and your tests will still be all green.

  • @AlekseyNew
    @AlekseyNew 9 лет назад +5

    It was pleasure to see. Very informative and practical. Thank you!

  • @caneesh
    @caneesh 7 лет назад

    Thank you for publishing this video. This is an asset to the world, because it teaches developers to write good quality code. Publish other videos addressing different challenges, its a great social service

  • @satoristeve
    @satoristeve 11 лет назад +2

    This was an exceptionally insightful and helpful video. I've been trying to teach myself good TDD recently, and one of my big hurdles has been dealing with legacy code (the other being figuring out how to begin testing MVC frameworks). Subscribed and I will be ordering your book. Thank you again for your excellent tutorial!

  • @sekharsoftware
    @sekharsoftware 8 лет назад +2

    I have not seen any other video which is as good as this one. Great. Cast more of these pls.

  • @sbern714
    @sbern714 9 лет назад

    My compliments! A very useful tutorial, and it's a joy to watch you code.

  • @abhilashmaipady4081
    @abhilashmaipady4081 2 года назад

    Such a great tutorial. Explained all the basics about the unit testing. Expecting these kind of videos again.

    • @SandroMancuso
      @SandroMancuso  2 года назад

      Thanks, Abhilash. You might want to look at our Codurance channel. All new videos are there. ruclips.net/user/codurance

  • @Artoooooor
    @Artoooooor 4 года назад

    Thank you. I deal with the legacy code quite often and I think I will find this lesson useful.

  • @VishalKotecha91
    @VishalKotecha91 9 лет назад

    It was awesome that way! Thank u so much for the wonderful session! *Appreciation*

  • @christofferklang
    @christofferklang 11 лет назад

    Reallt good video. Immediately adopted your naming scheme for tests, great solution to what I was missing most coming to Java from RSpec (the descriptions).
    Hope to see more videos in the future. Thanks alot.

  • @suckyboy
    @suckyboy 6 лет назад +8

    Thank you so much for the video. I think you just got me a new job

  • @manor1730
    @manor1730 6 лет назад

    Wow, great video even in 2018! Helped me understand some things I had been wondering about.

  • @7thAttempt
    @7thAttempt 9 лет назад

    Bravo! Thank you for taking the time to make this excellent video!

  • @CroydonDias
    @CroydonDias 9 лет назад +1

    This was fantastic! I really did learn a lot.

  • @odiernod
    @odiernod 11 лет назад

    Questo video è davvero fantastico, complimenti! Riesci a spiegare in un modo chiaro è semplice come si può, in effetti, rendere "testabile" un'applicazione "legacy". Lo faccio vedere a tutti i miei colleghi.

  • @jhonnytuba
    @jhonnytuba 6 лет назад +1

    You win my like, it's an excellent video. I look forward to seeing the next videos.

  • @lirazshay2635
    @lirazshay2635 4 года назад +1

    Such a great lesson!!! thanks Sandro!!!!

  • @saqibsaud9570
    @saqibsaud9570 7 лет назад

    Thank you, this video basically answered all my question.

  • @rajeshdhiman3272
    @rajeshdhiman3272 3 года назад +1

    It is awesome this way! Thank you so much.
    The tips you have given are great to start refactoring the legacy code and write UT.
    => Start testing from shortest branch to deepest branch.
    => Start refactoring from deepest branch to shortest branch
    Q1: should we more focus on loosely couple architecture and avoid static methods so that we can mock the dependencies?

  • @SandroMancuso
    @SandroMancuso  11 лет назад +2

    Here is the Github repository: github.com/sandromancuso/trip-service-kata
    Feel free to clone and play with it. :)
    When you do, push your solution to your own Github and send us the link adding a comment here. I would love to see different solutions and approaches to it.
    Thanks for watching the video.

  • @giladbaruchian7522
    @giladbaruchian7522 8 лет назад +1

    Very good video! it was very inspiring and i enjoyed it a lot. thanks!

  • @gpltaylor
    @gpltaylor 5 лет назад +1

    Truly a great video. Some very simple but powerful techniques that have helped me refactor any code base not just a Java application. Thank you Sandro for a great tutorial!

  • @elfoxini
    @elfoxini 3 года назад

    This was eye-opening for me, thanks

  • @AishwarMuthuraman
    @AishwarMuthuraman 10 лет назад

    Great video on refactoring legacy code.

  • @paulmudondo1041
    @paulmudondo1041 9 лет назад

    very well thought of and presented..

  • @LittleBobbyHasTables
    @LittleBobbyHasTables 10 лет назад

    Thank you very much for this awesome demonstration!!!

  • @surendermca
    @surendermca 9 лет назад

    excellent demo with real time code refactoring!

  • @dancing4nusphere
    @dancing4nusphere 8 лет назад +1

    best cast i seen about this topic

  • @KIRGOV
    @KIRGOV 8 лет назад

    Wooow lovely thanks a lot Sandro for this video
    I'll definitely give your video as food for thought and action to my team =)

  • @onebeartoe
    @onebeartoe Год назад

    Thanks for the presentation!

  • @SandroMancuso
    @SandroMancuso  11 лет назад

    Thanks Christoffer. Anyone coming from RSpec will definitely find the Java testing frameworks a little bit clunky. But with a few tweaks here and there we can make it look OK. A common alternative is to write the tests using Spock. I'm glad you liked the video.

  • @sathishkumarshanmugavel673
    @sathishkumarshanmugavel673 10 лет назад

    Good session. Very useful. Thanks a lot.

  • @Alexander92a
    @Alexander92a 3 года назад +1

    Very good video, thank you kind Sir.

  • @kke
    @kke 11 лет назад

    Paused to check if the noise is from your CPU fan or mine :) Great video.

  • @afshinsamia3328
    @afshinsamia3328 8 лет назад

    Thanks for this great video! learned a lot!

  • @avnerstr
    @avnerstr 11 лет назад +1

    great movie, you should do more !
    I really enjoyed it.

  • @ThomasLorenz1
    @ThomasLorenz1 5 лет назад +2

    Thank you so much! Great and very inform video. I'm new to Unit-Testing (C#) and this video is very helpful.

  • @maurihb
    @maurihb 11 лет назад

    Amazing video, as a not so experienced developer, I've been watching several talks of the theory behind all these, but finally watching it in action is very very helpful.
    Hope you continue making these screen casts.
    Could you recommend me some books/sites about:
    1) General IDE/Frameworks tips/tricks to improve productivity
    2) Java design patterns and taking advantage of the features of the language (as you did with your builder, and the addFriends(User... users) example)
    3) Re-factoring/Testing

  • @SandroMancuso
    @SandroMancuso  11 лет назад +1

    The last step, where I include Spring, was just a demonstration of what you can do in case you are already using Spring. I found it useful to add to the exercise since the vast majority of the Java projects use it.
    I just wanted to show how you can make Mockito work with the Spring @Autowired annotation. You can ignore the last step if you don't use Spring. An alternative would be to pass the TripDAO to the TripService constructor, in case you want to do dependency injection by hand.

  • @JICH2028
    @JICH2028 9 лет назад +1

    Second time to review. It is an awesome tutorial.

  • @PiotrKosmowski
    @PiotrKosmowski 11 лет назад

    Thank you. Piece of really good job.

  • @safajasim
    @safajasim 8 лет назад

    Great video ... thank you for sharing

  • @SandroMancuso
    @SandroMancuso  11 лет назад

    Thanks Johannes
    You are absolutely right. I totally forgot to refactor the ArrayList and use the Collections.emptyList() instead.

  • @SangpilBYUN
    @SangpilBYUN 11 лет назад

    Thank you.! I have learned from this video too much.

  • @SandroMancuso
    @SandroMancuso  11 лет назад

    My pleasure. Thanks.

  • @whatisthisnote
    @whatisthisnote 11 лет назад

    Enjoyed watching this!

  • @Mohamed-uf5jh
    @Mohamed-uf5jh 3 года назад +1

    Just excellent ! a good job

  • @gehbittedasgibtsned
    @gehbittedasgibtsned 3 года назад

    very nice test driven development

  • @LuigiAntonini
    @LuigiAntonini 11 лет назад

    Very very good video! Thank you!

  • @CharleyDC5R
    @CharleyDC5R 7 лет назад

    Masterpiece. Sharing this with all my team. Thank you Sandro!

  • @kaivansanghvi
    @kaivansanghvi 10 лет назад +3

    Awesome tutorial. Thank you!

  • @hichamouchker
    @hichamouchker 9 лет назад

    Thank you alot for this interesting tuto.. :-)

  • @sobreinquisidor
    @sobreinquisidor 7 лет назад +1

    What a beautyful piece of code! It was like whatching a paiting, enjoyable

  • @enricjaen3174
    @enricjaen3174 11 лет назад

    It would be nice if Sandro allowed others to modify his git code so people with further ideas like you could continue the refactoring.

  • @goncharenko85
    @goncharenko85 11 лет назад

    Awesome video! Thanks a lot.

  • @ashisha2z
    @ashisha2z 11 лет назад

    Thanks a lot for sharing this. Cheers

  • @yuskamify
    @yuskamify 10 лет назад

    Great video..
    Thanks a lot Sir

  • @rogerioliesenfeld9042
    @rogerioliesenfeld9042 11 лет назад

    An alternative is to use a mocking tool when writing the tests. PowerMock and JMockit are two such tools which allow the creation of unit tests without first changing the code to be tested, even if it calls static methods.

  • @SpiritOfIndiaaa
    @SpiritOfIndiaaa 4 года назад +1

    really good thanks a lot , how are you running "code coverage" command in elipse?

  • @realdn
    @realdn 11 лет назад

    Great video! Thanks a lot :)

  • @AntinKrauss
    @AntinKrauss 11 лет назад

    Really good video.

  • @davidbyrd4790
    @davidbyrd4790 10 лет назад +2

    Incredibly well done. Thanks so much for the effort.
    Feel free to do more!

  • @countdigi
    @countdigi 11 лет назад

    very well done!

  • @rockYhre
    @rockYhre 11 лет назад

    Great video, thanks for sharing. Can you post some additional refactoring resources you recommend?

  • @kameit00
    @kameit00 11 лет назад

    Thank you for this video. Just thank you...

  • @pierre-jeanmartin5621
    @pierre-jeanmartin5621 5 лет назад +1

    As many likes as subscribers, but you deserve more

  • @MehdiOkhovat-pf6fd
    @MehdiOkhovat-pf6fd Год назад +1

    finally a practical refactoring example, thank you very much. but I have a question. I think there was a better work around for isolating TripDAO static class. you could define an non static adapter class for it then wrap it, afterwards you could extract an interface from the wrapper class and inject it through TripService constructor.

    • @SandroMancuso
      @SandroMancuso  Год назад

      Thank you. Glad you liked it. The approach you suggested would certainly work, mainly if you really don't want or cannot change the TripDAO. I found it simpler just to add an instance method that uses the static. This way I can inject and mock the DAO, using the instance method instead. And if one day, all the references to the DAO are using the instance method, I could kill the static. Just a matter of preference. But both solutions would work fine.

  • @DetrickAli
    @DetrickAli 11 лет назад

    Awesome video, do you plan on making any more videos like this one?

  • @SandroMancuso
    @SandroMancuso  11 лет назад

    Thank you.

  • @huseyinaydogdu4381
    @huseyinaydogdu4381 8 лет назад

    Very nice video

  • @duffylord
    @duffylord 10 лет назад

    Very nice!

  • @dengan699
    @dengan699 7 лет назад

    wow awesome dude:)

  • @FranklinRibeiro
    @FranklinRibeiro 11 лет назад

    Initially, I thought "One hour and seventeen min?! No way!". One hour and seventeen minutes later... "Already over? Do not stop!" :)

  • @nickbarton3191
    @nickbarton3191 2 года назад

    Excellent.
    Shorter to deepest path.
    Any recommendations as to hierarchy order ? Bottom up ?

  • @sheljagupta1355
    @sheljagupta1355 7 лет назад

    Sir will you please upload more videos explaining the refactoring and testing of legacy code...
    It will be very hepful for us....
    Thanks for uploading this video.
    Its a request to plz upload more videos and practical explanation on refactoring...

    • @SandroMancuso
      @SandroMancuso  7 лет назад +1

      Thank you for watching. There are a few more videos here: codurance.com/publications/videos/

  • @alexsouza22
    @alexsouza22 7 лет назад +2

    Video foda mano. Parabéns!

  • @DaniloJr
    @DaniloJr 9 лет назад +3

    Sandro, it was very very nice! I'll be brave now. =) And your kata is a nice subject for a dojo. Abraão Alves Nelson Eldoro

  • @SandroMancuso
    @SandroMancuso  11 лет назад

    Thanks

  • @shaneduff9660
    @shaneduff9660 10 лет назад

    Thanks :)

  • @kwanjason4709
    @kwanjason4709 4 года назад

    this video is great ! however the last part of TripDao static to instance will break the function even it pass the test. It need to be initialize somewhere

  • @myhimalayanchants
    @myhimalayanchants 9 лет назад

    Yes this well though but why are we introducing auto wiring in production code ?
    This will change the start up behaviour ?

  • @SandroMancuso
    @SandroMancuso  10 лет назад

    I just added the link on the description of the video. RUclips doesn't allow me to add links when posting a comment.

  • @ChrisAthanas
    @ChrisAthanas Год назад

    13:09 audio clears up

  • @rikardsoderstrom835
    @rikardsoderstrom835 2 года назад

    Sadly you broke your "production code" in this video :( When moving the use of a global variable into your members argument list you cannot replace its default value with null all over the code base like that. It must be replaced with the value it had before you made it an argument, which in this case would be the global variable call. Yes it would stick in peoples eyes but it would be very visible everywhere that this global is now used as an argument, which would make it easy to fix when those other parts of code is to be updated.
    Anyways, keep up the good work :)

  • @dadinhoschannel4003
    @dadinhoschannel4003 7 лет назад +2

    Really awesome video!
    Does anyone knows where there are more videos like this?
    Thanks!