I am using clean architecture pattern from Jason Taylor template. It's a great pattern. I am struggling when trying to avoid exception in MediatR pipeline. I tried to convert to Result class. And it's challenging to make the code more C# friendly rather than functional way.
From C to C# then back to C, monads are fine if language is based on them. No reason to not use exceptions. Monad and exception code side by side is the same except you have unwrap opeator at the end.
I’m sorry but no. Of course one could argue that using exceptions for domain validation is an overkill as we are returning tons of data that is not needed in these situations. That’s true. And of course result pattern is more elegant and more suitable solution for this problem. But using performance as an argument in this discussion is absurd. I cannot imagine a real world example of an application that suffers from performance issues due to too many domain validation exceptions being thrown. If it is in fact the case then the problem lies in a poor solution architecture or execution, not in exceptions themselves. Don’t get me wrong, I am a fan of result pattern and often try to implement it. However I refuse to take performance as an argument in the exceptions vs result pattern debate.
Yes, and to resolve an error in the solution in production, you save thousands or hundreds of thousands of dollars because it was only 5 times slower when an exception is thrown, where this is ultimately a very rare occurrence and it should be Taken seriously, the stack trace allows the company's support team to quickly resolve the problem and save its application and money.
The point is that using exceptions for flow control is bad design. It's very often used for domain validation, because a lot of people don't think it matters. But it does. It does come as a surprise to many that exceptions are costly. If you are working on a smaller project, sure, it may not matter at all. As with everything, use what works best for you. However, in demanding applications, it is just so extremely unnecessary to design your application using practices that are notoriously slow.
@@MrNatsuDragneel For actual exceptions it’s a different story, obviously. When there’s something you haven’t prepared for that goes wrong, the stack trace is crucial. I think you are missing the point. I’m talking about avoiding exceptions for situations where it doesn’t make sense, like normal flow control. An example is validation.
The python docs should make it more clear right away that it’s not a good idea: ”A try/except block is extremely efficient if no exceptions are raised. Actually catching an exception is expensive.”
Thanks for the great suggestion! Exceptions are great for exceptional cases, when it's an unplanned outcome. There are definitely situations where it makes a lot of sense, but if it's a normal case that occasionally occurs then don't use exceptions.
Amazing Filip! 😊
Thanks! 😃
I am using clean architecture pattern from Jason Taylor template. It's a great pattern. I am struggling when trying to avoid exception in MediatR pipeline. I tried to convert to Result class. And it's challenging to make the code more C# friendly rather than functional way.
Nice, but you could have added the MemoryDiagnoser to see if there might be issues with GC.
@@martinkrajda5521 Thats very true! Thanks for pointing that out.
@@FilipEkberg Yes, please include memory allocation by default when doing benchmark
@@fakhrulhilalAlways! (Except this time when I forgot) 🫣
From C to C# then back to C, monads are fine if language is based on them. No reason to not use exceptions. Monad and exception code side by side is the same except you have unwrap opeator at the end.
I’m sorry but no. Of course one could argue that using exceptions for domain validation is an overkill as we are returning tons of data that is not needed in these situations. That’s true. And of course result pattern is more elegant and more suitable solution for this problem. But using performance as an argument in this discussion is absurd. I cannot imagine a real world example of an application that suffers from performance issues due to too many domain validation exceptions being thrown. If it is in fact the case then the problem lies in a poor solution architecture or execution, not in exceptions themselves.
Don’t get me wrong, I am a fan of result pattern and often try to implement it. However I refuse to take performance as an argument in the exceptions vs result pattern debate.
Yes, and to resolve an error in the solution in production, you save thousands or hundreds of thousands of dollars because it was only 5 times slower when an exception is thrown, where this is ultimately a very rare occurrence and it should be Taken seriously, the stack trace allows the company's support team to quickly resolve the problem and save its application and money.
The point is that using exceptions for flow control is bad design. It's very often used for domain validation, because a lot of people don't think it matters. But it does. It does come as a surprise to many that exceptions are costly.
If you are working on a smaller project, sure, it may not matter at all. As with everything, use what works best for you. However, in demanding applications, it is just so extremely unnecessary to design your application using practices that are notoriously slow.
@@MrNatsuDragneel For actual exceptions it’s a different story, obviously. When there’s something you haven’t prepared for that goes wrong, the stack trace is crucial.
I think you are missing the point. I’m talking about avoiding exceptions for situations where it doesn’t make sense, like normal flow control. An example is validation.
Don't use excedptions for flowcontrol? Tell that to the Python devs... :/
The python docs should make it more clear right away that it’s not a good idea:
”A try/except block is extremely efficient if no exceptions are raised. Actually catching an exception is expensive.”
Could you make a video with examples of where using exceptions would be appropriate?
Thanks for the great suggestion!
Exceptions are great for exceptional cases, when it's an unplanned outcome. There are definitely situations where it makes a lot of sense, but if it's a normal case that occasionally occurs then don't use exceptions.
you should sync your audio better to yourself
@@lextr3110 Where is it off? Shouldn’t be out of sync..
@lextr3110
Check your playback unit. Looks great on my end, and it’s probably timecode-synced during recording :)