Refactoring Messy to Testable Code in .NET (Part 1)

preview_player
Показать описание
In this series, we'll work through refactoring a messy .NET Core MVC web application with a lot of procedural code and no tests. When we're done, we'll have a more testable, object-oriented, extensible application, complete with unit tests and static code analysis wired up with a continuous integration loop with GitHub Actions. We'll cover topics like inversion of control via dependency injection, replacing conditionals with polymorphism, factory methods, sending messages between small objects, unit testing, naming, and much more!

What is Legacy Code?

“Code without tests is bad code. It doesn’t matter how well-written it is; it doesn’t matter how pretty or object-oriented or well-encapsulated it is. With tests, we can change the behavior of our code quickly and verifiably. Without them, we really don’t know if our code is getting better or worse.”

― Michael Feathers, Working Effectively with Legacy Code

Legacy code - code without tests - is everywhere. It is not unusual in practice to encounter entire applications with few or even no tests. It is perhaps just as common to find software with legacy tests that don't adequately test current software behavior.

As a professional software developer, you will inevitably work on code that is difficult to change and difficult to test. This will be because the original authors of the code (maybe even yourself) didn't understand how to write code that easy to change, didn't understand the importance of unit and integration testing, or perhaps didn't "have time" to write tests for code when it was originally written.

In this course, you'll clone a messy codebase and work step-by-step with me to get it back into shape - complete with tests, continuous integration, and static code analysis! We'll use concepts from object-oriented and functional programming to write cleaner, more testable, more modular code that is easy to change. This is much more than just "DRY" -we'll focus on concepts like:

Extensibility - we'll make it easier to implement new features
Loose coupling - we'll look at how "new is glue," "static cling," and side-effects lead to tightly-coupled software

Minimizing side effects - find out how writing more functional code can prevent unexpected behavior and decrease the chance you'll introduce bugs

Decreasing complexity - often the easiest code to write increases overall complexity - we'll look at how improving your ability to apply slightly advanced techniques can actually decrease the complexity of your application and make it much easier to reason about at different levels of abstraction

Improving naming - notoriously one of the most difficult tasks in software development - what do we call this thing?
Making smaller things - making smaller objects that inter-operate by sending and receiving messages make your codebase easier to maintain, organize, and reason about

Рекомендации по теме
Комментарии
Автор

What's the most interesting refactor you've worked on? Do you enjoy working on legacy code?

Hope everyone is doing well! The recording of this series is complete, and will comprise 10 videos over the next few weeks. I'm also bringing back 5-Minute Fridays this month, so stay tuned! Check out the description for GitHub link.

WesDoyle
Автор

Thank you Wes! I really look forward to the series and appreciate everything that you do!

tak
Автор

Wes great tutorial subject can really add to someones refactoring skills. Just an FYI the bootstrap lib is not in the distribution files when downloaded, easy fix just copy from another project and all fixed. Thank you for all your tutorials looking forward to following this one. Greg

thegemgarden
Автор

Hey Wes, long time no see! Guess you've been busy? ("Kept you waiting, huh?") I'd say that I took about 4 projects of mine on GitHub and refactored them to use clean architecture with hexagonal/onion structure when they were tightly coupled. That was a bit of a chore, thought not quite messy. Also, I started reworking the Dashboard project of yours from years ago to use Blazor and Dapper instead of Angular and Ef Core. Lots of learning keeping me busy.

johncraig
Автор

Thanks for this video! Nice one, just as was said... distracting by clicking and moving of mouse and maybey sometime use autoformatting - i dont know why it looks messy to me without it. But the maij idea of video is clear and thank You for it

satyres
Автор

You might want to stop moving your mouse and view around so much; scrolling, clicking, highlighting, etc.. This can be very jarring and distracting for some.

Chiramisudo