8 Tips To Write Clean Code - Refactoring Exercise

preview_player
Показать описание

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:

Read my Blog here:

Subscribe for more:

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 meaning
Рекомендации по теме
Комментарии
Автор

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

vikas
Автор

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.

davearkley
Автор

I really enjoy your videos, never fail to learn something new, or get a quick refresher - kudos to you, sir!

MrAlkazam
Автор

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👍

adammuridi
Автор

Very clear presentation. I have to give 1hr per day to watch this channel.

KhinSandarMyint-eqks
Автор

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.

MusicIsTheMaster
Автор

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!

mahir_labib
Автор

This is an amazing article that no one else teaches you!! Thankyou so much

nnasim
Автор

Really good video. Very helpful. Thanks.

msontrent
Автор

Great content. I love it. Thank Milan.

kodindoyannick
Автор

this was great! im all in for a weekly refactor fridays :D

silvertek
Автор

Thank you Milan. That's so insightful

BirhanNega
Автор

Need more Tips To Write Clean Code 1- async Task (HendlAsync) 2- productCache 3- what better save the product in json or [Serializable], 4- how to communicate better between client and server ( im using TcpLIstener and TcpLient) 5 - Tips Migrations. 5 - appsetings.json file. ))) thank you for your work. looking forward to more videos

angelldark
Автор

For me, the code before refactoring is clearer and much easier to understand than the code after refactoring.

firstlast
Автор

Thanks for sharing your time and knowledge,

marceloleoncaceres
Автор

Nice video. I like to be able to use pattern matching on result types so I'm is using an abstract base record and a bunch of inherited ones to represent the different results. It's as close as we can get to discriminated unions in C# without using third party code or implementing a Result type ourselves.

soverain
Автор

4:35
Use the [NotNullWhen(true)] attribute on the order parameter instead in the "IsProcessable" method instead

modernkennnern
Автор

Loved the vid. Really neat and straightforward. Congrats.
P.S.: having a Java background, the method with capital I in the beginning, made my itchy. 😂

danieljunior
Автор

Clean Code and Performance optimization.... two classic phases of a Junior dev jouney to become a Senior dev.

vivekkaushik
Автор

Actually I used the same concept in my previous projects, but I made it more generic with status enum which helped in easy checks

vkishan