Тёмный

How Guard Clauses Can Make Your Code Better 

Milan Jovanović
Подписаться 102 тыс.
Просмотров 13 тыс.
50% 1

Опубликовано:

 

29 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 46   
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Want to master Clean Architecture? Go here: bit.ly/3PupkOJ Want to unlock Modular Monoliths? Go here: bit.ly/3SXlzSt
@cclementson1986
@cclementson1986 11 месяцев назад
Defensive programming?
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Yes it is
@cclementson1986
@cclementson1986 11 месяцев назад
@@MilanJovanovicTech Awesome! I love the promotion of defensive programming. Thanks for the tutorial. I do C++ and defensive programming is a must! Love to see it being implemented in other languages.
@saddamhossaindotnet
@saddamhossaindotnet 11 месяцев назад
I loved the way you explained it. Just simply awesome! Thanks for sharing this video, my friend!
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Glad you liked it!
@ruekkart
@ruekkart 11 месяцев назад
At 11:31, it might be a good idea to check if FirstName and LastName (as Value Objects) are not null within the ChangeName method. While their inner "Value" property is assured to be non-null or non-empty, the objects themselves could still be null
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Turn on "Nullable reference types" - the compiler will warn you if you try pass in a null. Otherwise, there is no chance that a null will make it to the domain.
@vincentjacquet2927
@vincentjacquet2927 11 месяцев назад
​@@MilanJovanovicTech Unfortunately, the compiler does not enforces it, neither on the caller or the callee side. So any code calling your function may pass in a null reference. Therefore you still must guard against null, even when Nullable reference types is turned on. But this is the only guard you still have to write. The value object with the more complete validation is a nice touch, even if I would have made a single PersonName with both firstName and lastName, as the two are tightly coupled.
@fifty-plus
@fifty-plus 11 месяцев назад
Be careful in your example, connectionString wasn't an argument, your expectation was that it was set in the environment but you were guarding a side effect from that. Nice job suggesting the packages, much better than hand baking a lot of code. The new dotnet guards are a nice addition too.
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
What other way do I have to validate the environment?
@_iPilot
@_iPilot 11 месяцев назад
Explicit null check also suppresses all "possible NRE" warnings on using checked variable. Guard classes are hiding that check from the code analyzer, but attribute `[NotNull]` for value argument in method `IsNotNullOrWhiteSpace` can instruct analyzer that the string is fine when the Guard method does not throw an exception.
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Yes
@laviritel
@laviritel 11 месяцев назад
I was wondering when I watched your pragmatic course, why you don’t use the guard clauses there. In our team, we are using ardalis library, and we check almost everything, even dependency services passed to constructor.
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
I like using guard clauses, but I prefer the result pattern more. It does make me wonder what is the point of some guard clauses. For example services from DI. When will they be null?
@sebastianszafran5221
@sebastianszafran5221 2 месяца назад
I know that it's a rather silly example, but FirstName and LastName still can technically be null, right? I don't see anything preventing from writting following code FirstName firstName = null; person.UpdateFirstName(firstName);
@MilanJovanovicTech
@MilanJovanovicTech 2 месяца назад
Nullable reference types can prevent code like that from compiling.
@darkopz
@darkopz 11 месяцев назад
I’ll argue the Throw Extensions can cause confusion. It’s a bit of an oxymoron to dereference the connection object with a ThrowIfNull.
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
It's also less obvious what's going on versus the explicit guard approach
@aisonjackmendoza7709
@aisonjackmendoza7709 11 месяцев назад
Little by little I'm learning new things and starting to understand how and why to use different techniques. Can I just ask, is it essential to implement eventhandlers for the domain?
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
It's not, but it's something I prefer using
@porcinetdu6944
@porcinetdu6944 11 месяцев назад
I think that the evaluation of the param name for CallerArgumentExpression is done at compile time instead of at runtime. Not 100% sure
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
You are correct there!
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Docs for reference: learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/caller-argument-expression
@TurgayTuerk
@TurgayTuerk 11 месяцев назад
Thank you Milan. Very instructive as always.
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Always welcome, glad you enjoyed this one :)
@manasabbi
@manasabbi 11 месяцев назад
How it's different from Fluent Validation, am i miss something here?
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
How are exceptions different from FluentValidation?
@winchester2581
@winchester2581 11 месяцев назад
IMO FluentValidation is more fittable in complex scenarious when you need to validate complex objects. Guards just throw exceptions, behind the scenes it's just the same exception throwing. I see no reason to use it, for example, for extracting connection string because it's simple object and it's validated in one place
@phisakel1
@phisakel1 11 месяцев назад
Swift language has a native guard statement 😄
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
That's awesome. Got a link to some docs?
@NickSteffen
@NickSteffen 11 месяцев назад
.NET has built in guard clauses now too. You can use ArgumentNullException.ThrowIfNull(value) for null checks. You can use ArgumentException.ThrowIfNullOrEmpty(stringValue) for strings. In .NET 8 they are adding a bunch of new ones as well under an ArgumentOutOfRangeException. The best part is that the null checks will resolve nullable reference compiler warnings whereas the make your own ensure class method does not as the compiler doesn’t recognize that as a null check.
@pilotboba
@pilotboba 11 месяцев назад
The stack trace on that guard clause will point to the guard clause, correct? I assume the gaurdclause and throw library will cause the same thing. Also, i would prefer the result pattern for value object creation as well, which Vladimir Khorikov demonstrates in several of his DDD and Validation Fundementals courses on pluralsight.
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Yes, they'll have the same stack trace. And I'm working on a Result pattern video soon :)
@andreyz3133
@andreyz3133 11 месяцев назад
sad thing is that if you using nullable reference types enabled, ide will treat variable as nullable and you have to use ! operator
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
There are ways to help the compiler 😁
@sunzhang-d9v
@sunzhang-d9v 11 месяцев назад
👍👍👍👍👍👍, expect ------------ Result pattern video
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Incoming 😁
@mwaseemzakir
@mwaseemzakir 11 месяцев назад
Loved it Milan❤
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Awesome! 😁
@techpc5453
@techpc5453 11 месяцев назад
@MilanJovanovicTech
@MilanJovanovicTech 11 месяцев назад
Getting creative now?
Далее
How to Use Value Objects to Solve Primitive Obsession
13:54
Don't Make This Common Domain Events Mistake
11:27
Просмотров 9 тыс.
DAXSHAT!!! Avaz Oxun sahnada yeg'lab yubordi
10:46
Просмотров 442 тыс.
Bearwolf - GODZILLA Пародия Beatrise
00:33
Просмотров 254 тыс.
3 .NET "Best Practices" I Changed My Mind About
10:16
Просмотров 103 тыс.
Stop using trivial Guard Clauses! Try this instead.
11:57
5 Awesome Refactoring Tips To Clean Up Your Code
19:23
Why .NET's memory cache is kinda flawed
14:13
Просмотров 56 тыс.
The Guard Clause in C# using the Throw NuGet Package
35:40
8 Tips To Write Clean Code - Refactoring Exercise
16:06
Throw, the only .NET guard clause library you need
17:22
Cleaner Code: 3 Ways You Can Write Cleaner Code
7:41
Are events in C# even relevant anymore?
16:19
Просмотров 169 тыс.