8 Tips To Write Clean Code - Refactoring Exercise

34,493
51
2023-06-23に共有
Get the source code for this video for FREE → the-dotnet-weekly.ck.page/clean-code
☄️ 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: www.patreon.com/milanjovanovic

Writing Clean code should be your goal when you sit in front of a keyboard. However, making your code clean is a matter of experience and practice. I will show you 8 tips to write clean code that I learned in my career.

Join my weekly .NET newsletter:
www.milanjovanovic.tech/

Read my Blog here:
www.milanjovanovic.tech/blog

Subscribe for more:
   / @milanjovanovictech  

Chapters
0:00 Code Kata starting point
0:39 Tip 1 - Early Return Principle
2:33 Tip 2 - Merge multiple if statements
3:23 Tip 3 - Use LINQ Any instead of checking the Count
3:43 Tip 4 - Replace boolean expression with descriptive method
4:52 Tip 5 - Prefer throwing custom exceptions
8:16 Tip 6 - Replace magic numbers with a constant
9:31 Tip 7 - Replace magic strings with an enum
11:00 Tip 8 - Use a result object that conveys meani

コメント (21)
  • @vikas4483
    1. Fail fast/early return/guard clause 2. Use custom exception for better readability 3. Replace magic numbers with constants 4. User result object for better messaging with methods for all return cases
  • Nice Milan. I'd personally add a status (another Enum perhaps or an error code) on the ProcessOrderResult so I can switch on it in code calling the Process method. If I just want a message (for UI or logging) then I probably don't want any flow control around the result and the string would suffice, but if I do (say to trigger a recovery action on failure) then I feel it's better to switch on status rather than the message text. Particularly important if you ever localize the messages.
  • @MrAlkazam
    I really enjoy your videos, never fail to learn something new, or get a quick refresher - kudos to you, sir!
  • Great Video Milan!! I really enjoyed it and learned a lot. Please keep making more content like this for us juniors trying to level up. Cheers👍
  • @mahir_labib
    Great content! Perfec video for beginner to advanced tutorial, giving the full picture of refactoring. Shows a great deal of planning before recording the video. Cheers!
  • Nice clear presentation. I think an important missed tip is ensuring there is sufficient unit tests in place covering the code before refactoring. That way as you change the code you can ensure its all still working by running them after each change. Might be overkill for this simple example but for more complicated code its a must.
  • This is an amazing article that no one else teaches you!! Thankyou so much
  • @silvertek
    this was great! im all in for a weekly refactor fridays :D
  • @firstlast493
    For me, the code before refactoring is clearer and much easier to understand than the code after refactoring.