Why replace if statements with Objects
Вставка
- Опубліковано 7 чер 2024
- Examples of when replacing if statements with objects yields better software.
Support my Work 🤝 / raw_coding
Buy my Courses 📚 learning.raw-coding.dev
Buy my Merch 🛒 shop.raw-coding.dev
Join the Community 💬 / discord
Tweet Tweet 📣 / anton_t0shik
Tune in LIVE! 🎥 / raw_coding
#programming #csharp
Thank you Moses. Please keep guiding me through the sea of programming.
🤣
hahahahahahha
Mind blown. I had just never thought about it that way. Thanks.
Love to see a video about injecting interfaces in DI with a bit of reflection or something smart with functional programming.
aaa white theme. I'm blind now
Hi, it seems that discord link doesn't work anymore
if you have an if with 2 options there's really no need to do that. Each class you create, each file you have to open increases cognitive load needed to read the code, and then instead of having one class with concise logic, you end up with 3 files that you have to open to understand the flow. Needless splittingof you code which doesn't give you much in return. Just stay with a simple if, don't overengineer.
Mhmmm, so when you build an Api, you have 1 endpoint, call it for each parts of your application and then use if statements on the payload to determine which logic to execute further? (1 big file = no cognitive load?)
No you split it up into manageable chunks. I explain that you want to split it up based on business functions admin/customer etc… as a symptom for a module doing too much is alot of if statements. Splitting it up is also safer because introducing a change wont break other parts of your application.
@@RawCoding If you split up business logic within on flow, something that is logically cohesive, you introduce more congitive load than needed.
>Mhmmm, so when you build an Api, you have 1 endpoint, call it for each parts of your application and then use if statements on the payload to determine which logic to execute further?
Depends. If there's a totally different flow depending on the payload, i'd use differnt objects/functions to handle that or even use different endpoints. But the if would still be necessary, whether I'd check the payload, or make someone choose an endpoint or even use something like keyed services.
If it's something like subset/superset, e.g. user who executes some action and we do additional stuff depending on whether they're an admin or not, I'd have a User.cs class with a flag and then handle that request with a specific user and check whether they have right or not. In an if.
Great video, as it has become more likely like typescript.
This is good stuff
Embedded interface. First time seeing this. Not sure what to think of it.
Haha, yeah that was just a quick thing dont do that
2nd example is kind of an overkill. I get the point, but in this case, passing a selector function as a parameter was enough.
So you’re still replacing the if statement with an object, the downside of that is that you’re expecting the selector function to be the only thing that is different between the two.
Could you please make a video about autofac?
how to create problems out of thin air
The first 2 examples are completely fair imo, I would almost never go for the 3rd one though
Think of the 3rd example as a kin to Regex, where our expression was a tuple
KISS
@RawCoding this is just not necessary in most cases and in some cases it cannot be done. Even the founders of stack overflow have said they see no reason for it and prefer if statements
I had to check if it was the first of april - but no
😐
My man DARK MODE please... at least a warning that viewer retinas are about to be scorched. 🫣