5 Awesome Refactoring Tips To Clean Up Your Code

Ғылым және технология

Get the source code for this video for FREE → the-dotnet-weekly.ck.page/ref...
☄️ Master the Modular Monolith Architecture: bit.ly/3SXlzSt
📌 Accelerate your Clean Architecture skills: bit.ly/3PupkOJ
🚀 Support me on Patreon to access the source code: / milanjovanovic
Check out my courses: bit.ly/3PupkOJ
Refactoring is a technique for restructuring existing code without changing its behavior. You can think of refactoring as a series of small code transformations. One change (refactoring) does little. But a sequence of refactors produces a significant transformation. There's no better way to learn refactoring than practicing. So, I prepared a refactoring exercise for you. Today, I'm going to refactor some poorly written code. And I'll show you 5 awesome refactoring techniques along the way:
- Extract method
- Extract interface
- Extract class
- Functional code
- Pushing logic down
5 Awesome C# Refactoring Tips
www.milanjovanovic.tech/blog/...
Join my weekly .NET newsletter:
www.milanjovanovic.tech
Read my Blog here:
www.milanjovanovic.tech/blog
Chapters
0:00 Starting point
2:52 Refactoring the Validation logic
6:15 Refactoring towards Dependency Injection
8:01 Refactoring the Calculation logic
15:57 Wrapping up the Refactoring
16:51 Running the tests after Refactoring
18:07 A few more improvement points

Пікірлер: 86

  • @MilanJovanovicTech
    @MilanJovanovicTech6 ай бұрын

    Get the source code for this video for FREE → the-dotnet-weekly.ck.page/refactoring

  • @mrwalkan

    @mrwalkan

    6 ай бұрын

    Hello sir, Could you please make video on static keyword's appropriate use-cases?

  • @faridhuseynli265

    @faridhuseynli265

    5 ай бұрын

    Where is source code?

  • @antonmartyniuk
    @antonmartyniuk5 ай бұрын

    This video is a real find for developers, especially young developers. So much useful info about how to write a cleaner code without a need to come up with this in a few years of own experience (like me)

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Glad you enjoyed it!

  • @jinx88909
    @jinx889096 ай бұрын

    Great video: the way you've approached each refactor and used the IDE to refactor is really useful! I love refactoring and do enjoy the feeling I get when I've finished. I always share these kinds of videos with my colleagues as I find they help people to adopt the refactoring mindset. Thank you for putting this together.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Glad you liked it!

  • @1valddev133
    @1valddev1336 ай бұрын

    Awesome video, thanks Milan!

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    My pleasure!

  • @FPontiDev
    @FPontiDev6 ай бұрын

    Oh! You have made my day! Thanks!

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Happy to help! 😁

  • @kodindoyannick5328
    @kodindoyannick53284 ай бұрын

    Great video about refactoring. I learnt again more much. Thank you Milan

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    My pleasure!

  • @baranacikgoz
    @baranacikgoz6 ай бұрын

    This was a refactoring masterclass for sure!

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Thank you very much!

  • @thanhdungnguyen6816
    @thanhdungnguyen68165 ай бұрын

    Great video, thank you so much.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Glad it was helpful!

  • @ivandrofly
    @ivandrofly5 ай бұрын

    Good one, thanks :)

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Glad you liked it!

  • @VirtueelGamingNL
    @VirtueelGamingNL6 ай бұрын

    Didnt know tuples could be used like that amazing!

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Tuples are pretty cool

  • @AboutCleanCode
    @AboutCleanCode5 ай бұрын

    Nice video on how simple refactoring steps can greatly improve code readability and understandability! One small point: as the logic now factored out into CreditLimitCalculator was an implementation detail earlier, I would have preferred keeping it internal to the assembly by making the class internal 😉

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Good point! I didn't want to bother with that stuff as it's a practice example. But makes a lot of sense on a real-world project.

  • @vikas4483
    @vikas44836 ай бұрын

    1. Unit testing is a guard to make sure we not refactoring wrongly 2. Convert Boolean statement to expression 3. Convert magical numbers to const variables for better readability 4. Dependency injection for repository classes so that we can change underlying database knowledge if needed in future along. Also to reduce external dependencies for business logic 5. Business logic should not be part of controller or repository hence achieving single responsibility principle benefits. 6. Use Tuple when we want method to return multiple values of different data types and do not want to create a new type for just single place use. 7. Switch expression.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Nice 👌

  • @xra1se-music909
    @xra1se-music9096 ай бұрын

    Every client is a "very important client"! :)

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    True!

  • @giutargreenday
    @giutargreenday6 ай бұрын

    I can't agree with the improvements for 'IsValid' refactor. While you saved like 10 lines of code, it's become much less readable IMO. I don't see a need for such minimalism, when it impacts your knowledge of what's happening at a glance. The previous implementation was much faster to comprehend than the refactored one. Just my two cents, other than that, great video as always!

  • @Targeting-Must-End

    @Targeting-Must-End

    6 ай бұрын

    DRY vs DAMP

  • @jinx88909

    @jinx88909

    6 ай бұрын

    Yeah, I was thinking the same. I have done similar code in the past and when I come back to it I find I struggle to read it and fully understand it. For me, I would even consider extracting out each check into its own method and then you can read the method names, not the syntax. You could argue however that's overkill.

  • @VirtueelGamingNL

    @VirtueelGamingNL

    6 ай бұрын

    ​@@jinx88909this is such a simple method the name alone should be sufficient to know what is happening. I do agree with more complex logic.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    I typically care more about what it does (validation) than what's inside. If what's inside is complex, I can give the method a better, more descriptive name. I don't have a problem with reading bool expressions like this, so that's why I prefer this approach.

  • @vikas4483

    @vikas4483

    6 ай бұрын

    @@MilanJovanovicTechadding to it, also such methods helps us in debugging by simply using step over as I can understand what it is doing by just its name.

  • @gabrielporto3936
    @gabrielporto39365 ай бұрын

    Awesome video, Milan! Can you make a video explaining the strategy pattern?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Good idea

  • @gabrielporto3936

    @gabrielporto3936

    5 ай бұрын

    ​@@MilanJovanovicTechor better still, you coud do a series talking about desing patterns hahaha!

  • @YetMoreStuff-fw1fg
    @YetMoreStuff-fw1fg4 ай бұрын

    Great video Milan, thanks for posting. Just one question: in practice would you be running the tests more often, in fact after virtually every functional change, to ensure you haven't broken anything?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    4 ай бұрын

    Depends on how complex the refactoring is

  • @ZF-CVA
    @ZF-CVA5 ай бұрын

    Great video as always! But how do you solve the dependency on Time.Now in the function youv'e extracted it into?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Create a service like IDateTimeProvider

  • @fabiomaistro4338
    @fabiomaistro43386 ай бұрын

    Great video as always Milan. About the Customer "IsUnderCreditLimit" method, I usually prefer the calculated property in these cases where the calculation is easy, fast and doesn't require additional parameters (relies only on existing properties of the entity itself). What do you think?

  • @user-xm7sh3vw8o

    @user-xm7sh3vw8o

    6 ай бұрын

    ? show code

  • @jeansimonlavoie

    @jeansimonlavoie

    6 ай бұрын

    @@user-xm7sh3vw8o something like that bool MyProp => AnotherProp >= 12345 ; Or string FullName => “{FirstName} {LastName}”

  • @john_doe_97

    @john_doe_97

    6 ай бұрын

    It just adds additional boilerplate code for almost no performance benefits... There is even a refactoring pattern called "Replace temp with query" which suggest avoiding it. Also, your property may depend on mutable fields, and then you would have to recalculate it. However, if the property represents a state of an object and not a behavior, then it is probably fine

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Properties are also methods when compiled, so it's the same idea. 😁

  • @Paul-uo9sv
    @Paul-uo9sv5 ай бұрын

    Hey man, video idea for you, what's the best way to version api endpoints for front end apps. Sometimes not all apps get updated on the front end and some of them do so both old and new endpoints have to be used what's the best way to version these. Also how to keep track which APA version is being used by front and applications and I'm talking multiple applications Android iOS web apps

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    I think that should be dictated by the backend, and there are mechanisms to support this

  • @ricodomonkos3053
    @ricodomonkos30535 ай бұрын

    Nice tutorials, Milan. But please reposition your mike so it captures your voice directly instead of the voice of your room. :)

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    I'm not hearing what you're hearing 😅

  • @ricodomonkos3053

    @ricodomonkos3053

    5 ай бұрын

    @@MilanJovanovicTech Used to work in the music industry as a sound engineer.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    @@ricodomonkos3053 Thought so. Impressive. I'll see what I can do, appreciate the advice very much!

  • @vikas4483
    @vikas44836 ай бұрын

    Can you create a video on strategy pattern?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Will see

  • @vikas4483

    @vikas4483

    6 ай бұрын

    @@MilanJovanovicTech thanks. Waiting

  • @cnz0183
    @cnz018321 күн бұрын

    Could you use a smart enums instead of a simple enums for the company type property? The calculate method depends of the type of client so I would encapsulate this method in theus enums

  • @MilanJovanovicTech

    @MilanJovanovicTech

    21 күн бұрын

    We could've done that, yeah

  • @NafisKhalilov
    @NafisKhalilov6 ай бұрын

    I would add parameterless constructor with default implementation instansiation so it does not break arrange steps in my tests, not sure how it goona look like in this primary constructor thing though

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Parameterless ctor can cause silent failures though

  • @NafisKhalilov

    @NafisKhalilov

    6 ай бұрын

    @@MilanJovanovicTech can you clarify what kind of failures they can cause

  • @vikas4483

    @vikas4483

    6 ай бұрын

    @@NafisKhalilov What is your code is using variables which has got initialized using overloaded constructors. Just a thought. have to test it.

  • @NafisKhalilov

    @NafisKhalilov

    6 ай бұрын

    @@vikas4483 I dont know maybe any client code that uses that class. Overloaded ctor needed to not break backwards compatibility

  • @PablofMorales
    @PablofMorales5 ай бұрын

    There are a couple of things that still require some "refactor" for example isValid It should have a method for each conditions instead of having all the logic in one global method for example it should be return isEmailValid(args...) && isTheUserMature(args..) && isWhatever() So you avoid need to add comments on your code explaning what the logic is so in case, of someone want to add a regex to validate the email instead they can change one method or for example have a different rule for age depending on the country it will be better besides readability

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    I figured it would be too much

  • @PablofMorales

    @PablofMorales

    5 ай бұрын

    ​@@MilanJovanovicTech hehe maybe, but that is part of been explicit about what your code does. Too much for the video will be adding a Validation Class for each Type which will depends on the level of abstraction that your application is having. But as everything this is subjective to the team point of view.

  • @alexsandrotabosa4461
    @alexsandrotabosa44616 ай бұрын

    What your theme for visual Studio?

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    ReSharper

  • @alexsandrotabosa4461

    @alexsandrotabosa4461

    6 ай бұрын

    @@MilanJovanovicTech so Good, tkss

  • @arunprasadchief
    @arunprasadchief6 ай бұрын

    The validation code just become too complex to read.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    I didn't think so, at the time

  • @madd5
    @madd56 ай бұрын

    you look like a serial refactorer in the thumbnail

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    😂

  • @user-xm7sh3vw8o
    @user-xm7sh3vw8o6 ай бұрын

    First 😂

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Tough competition this time 🤣

  • @ahaf1605

    @ahaf1605

    6 ай бұрын

    Hahaha

  • @sashapokimica2044
    @sashapokimica20446 ай бұрын

    Am I missing something? How the f... a && b && (c || d) && e became a && b && c && d && e? :) In your IsValid method before the last refactoring in return you have allowed email to have either @ OR . characters, but after refactoring you explicitly want email to have @ AND . So either your tests were not covering valid email in previous implementation or previous implementation was not valid and it allowed emails without dot or @ :) It bothered me through entire video until the end because it's really great video and I expected that your tests at the end will fail just to say "I knew it' :D

  • @sauravbhatta5303

    @sauravbhatta5303

    6 ай бұрын

    It was bunch of “||” before and if u flip it then u need to negate using “&”. If u notice carefully there is a green line on that “If”; which the IDE suggests u to negate. He just accepted the suggestion. So there is nothing wrong saying u like it, or saying u didn’t unlike it.

  • @MilanJovanovicTech

    @MilanJovanovicTech

    6 ай бұрын

    Isn't it a !Contains condition at the start?

  • @sashapokimica2044

    @sashapokimica2044

    5 ай бұрын

    @@MilanJovanovicTech Ah nema sanse sada da se vracam :). Nebitno. Odlican si malac, samo tako nastavi!

  • @techpc5453
    @techpc54535 ай бұрын

  • @MilanJovanovicTech

    @MilanJovanovicTech

    5 ай бұрын

    Long time no wave 👋

Келесі