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... - Наука
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!
I wish someone showed me this 9 years ago, I would have kept so many legacy code revamp jobs that I attempted and failed
Definitely one of the best videos about testing and refactoring, thank you !
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’.
Yes
This kind of thing is still not taught in any school
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.
Definitely one of the best videos about testing and refactoring.
What one learns from this video is what beginners learn after years of experience. Thank you very much for this video... Truely grateful.
Thank you.
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!
+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!
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
Feel like ive been searching for this since forever
There's not many refactoring tutorials as good as this! Thx.
Thank you
Very good insights on how to deal with coupled legacy code, thanks man! 👏👏
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!
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.
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!
Finally took the time to do this kata. It's great to see your pov afterwards. Thanks for sharing!
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.
6:55 the seam technique is so good. actually solves 90% of the legacy code testability issues :D
Thanks for calling attention to that one! It is really useful.
Feels a bit like test induced damage to introduce inheritance like that, but you can always refactor it later, I guess
@@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.
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.
It was pleasure to see. Very informative and practical. Thank you!
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
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!
I have not seen any other video which is as good as this one. Great. Cast more of these pls.
My compliments! A very useful tutorial, and it's a joy to watch you code.
Such a great tutorial. Explained all the basics about the unit testing. Expecting these kind of videos again.
Thanks, Abhilash. You might want to look at our Codurance channel. All new videos are there. ruclips.net/user/codurance
Thank you. I deal with the legacy code quite often and I think I will find this lesson useful.
It was awesome that way! Thank u so much for the wonderful session! *Appreciation*
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.
Thank you so much for the video. I think you just got me a new job
Wow, great video even in 2018! Helped me understand some things I had been wondering about.
Bravo! Thank you for taking the time to make this excellent video!
This was fantastic! I really did learn a lot.
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.
You win my like, it's an excellent video. I look forward to seeing the next videos.
Thank you
Such a great lesson!!! thanks Sandro!!!!
Thank you, this video basically answered all my question.
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?
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.
Very good video! it was very inspiring and i enjoyed it a lot. thanks!
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!
This was eye-opening for me, thanks
Great video on refactoring legacy code.
very well thought of and presented..
Thank you very much for this awesome demonstration!!!
excellent demo with real time code refactoring!
best cast i seen about this topic
Wooow lovely thanks a lot Sandro for this video
I'll definitely give your video as food for thought and action to my team =)
Thanks for the presentation!
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.
Good session. Very useful. Thanks a lot.
Very good video, thank you kind Sir.
Paused to check if the noise is from your CPU fan or mine :) Great video.
Thanks for this great video! learned a lot!
great movie, you should do more !
I really enjoyed it.
Thank you so much! Great and very inform video. I'm new to Unit-Testing (C#) and this video is very helpful.
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
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.
Second time to review. It is an awesome tutorial.
Thank you. Piece of really good job.
Great video ... thank you for sharing
Thanks Johannes
You are absolutely right. I totally forgot to refactor the ArrayList and use the Collections.emptyList() instead.
Thank you.! I have learned from this video too much.
My pleasure. Thanks.
Enjoyed watching this!
Just excellent ! a good job
very nice test driven development
Very very good video! Thank you!
Masterpiece. Sharing this with all my team. Thank you Sandro!
Awesome tutorial. Thank you!
Thank you alot for this interesting tuto.. :-)
What a beautyful piece of code! It was like whatching a paiting, enjoyable
It would be nice if Sandro allowed others to modify his git code so people with further ideas like you could continue the refactoring.
Awesome video! Thanks a lot.
Thanks a lot for sharing this. Cheers
Great video..
Thanks a lot Sir
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.
really good thanks a lot , how are you running "code coverage" command in elipse?
Great video! Thanks a lot :)
Really good video.
Incredibly well done. Thanks so much for the effort.
Feel free to do more!
very well done!
Great video, thanks for sharing. Can you post some additional refactoring resources you recommend?
Thank you for this video. Just thank you...
As many likes as subscribers, but you deserve more
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.
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.
Awesome video, do you plan on making any more videos like this one?
Thank you.
Very nice video
Very nice!
wow awesome dude:)
Initially, I thought "One hour and seventeen min?! No way!". One hour and seventeen minutes later... "Already over? Do not stop!" :)
Excellent.
Shorter to deepest path.
Any recommendations as to hierarchy order ? Bottom up ?
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...
Thank you for watching. There are a few more videos here: codurance.com/publications/videos/
Video foda mano. Parabéns!
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
Thanks Danilo.
Thanks
Thanks :)
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
Yes this well though but why are we introducing auto wiring in production code ?
This will change the start up behaviour ?
I just added the link on the description of the video. RUclips doesn't allow me to add links when posting a comment.
13:09 audio clears up
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 :)
Really awesome video!
Does anyone knows where there are more videos like this?
Thanks!