High Definition Standard Definition Theater
Video id : HTrIbVZecOE
ImmersiveAmbientModecolor: #a87a65 (color 2)
Video Format : (720p) openh264 ( https://github.com/cisco/openh264) mp4a.40.2 | 44100Hz
Audio Format: 140 ( High )
PokeEncryptID: 73025dfd2b3f2921ef9b5ae159214f4faa73df53c8080e025d01f16bc836242e0319f4aa63ab421b1b54575fda2d0075
Proxy : cal1.iv.ggtyler.dev - refresh the page to change the proxy location
Date : 1732943722068 - unknown on Apple WebKit
Mystery text : SFRySWJWWmVjT0UgaSAgbG92ICB1IGNhbDEuaXYuZ2d0eWxlci5kZXY=
143 : true
Favor Guard Clauses Over if-else
Jump to Connections
6,454 Views • Dec 2, 2023 • Click to toggle off description
Do you consider using guard clauses instead of nesting code?
Here is one typical implementation of the factorial function based on if instructions, with four levels of nesting. It is mixing error conditions, trivial solutions and full implementation.
That coding style is bad, and we will now improve it by applying guard clauses.
Test error conditions first, and fail early in the method - at its very beginning. That has removed one nesting level.
Then consider trivial cases. If there is a shortcut for some argument values, detect them early and return the trivial solution without doing any work.
Only then step to doing actual work, sustaining as many nesting levels as it requires naturally - one additional level in this case.
This coding style will often give you an idea how to turn the entire method, including all guards, into a single expression.
Can you believe that this code is doing everything the original function did?

✅🔔 Become a patron ► www.patreon.com/ZoranHorvat
✅🔔 Subscribe ►    / @zoran-horvat  
⭐ Learn more from video courses:
Beginning Object-oriented Programming with C# ► codinghelmet.com/go/beginning-oop-with-csharp
⭐ Collections and Generics in C# ► codinghelmet.com/go/collections-and-generics-in-cs
⭐ Making Your C# Code More Object-oriented ► codinghelmet.com/go/making-your-cs-code-more-oo
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⭐ CONNECT WITH ME 📱👨

🌐Become a patron ► www.patreon.com/ZoranHorvat
🌐Buy me a Coffee ► ko-fi.com/zoranhorvat
🗳 Pluralsight Courses ► codinghelmet.com/go/pluralsight
📸 Udemy Courses ► codinghelmet.com/go/udemy
📸 Join me on Twitter ► twitter.com/zoranh75
🌐 Read my Articles ► codinghelmet.com/articles
📸 Join me on LinkedIn ► www.linkedin.com/in/zoran-horvat/
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
👨 About Me 👨
Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my YouTube channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️RIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.

#csharp #dotnet
Metadata And Engagement

Views : 6,454
Genre: Science & Technology
Uploaded At Dec 2, 2023 ^^


warning: returnyoutubedislikes may not be accurate, this is just an estiment ehe :3
Rating : 4.813 (19/388 LTDR)

95.33% of the users lieked the video!!
4.67% of the users dislieked the video!!
User score: 93.00- Overwhelmingly Positive

RYD date created : 2024-06-08T17:33:56.281739Z
See in json
Tags
Connections

31 Comments

Top Comments of this video!! :3

@brendonlantz5972

11 months ago

Someone mentioned this to me in a code review, and it really had a big impact on my code. It might seem like rearranging the same logic, but you are really separating out conditions. Every additional nested "If" level is a condition you have to keep in your mind when reading the code. By reducing nesting it really helps a codebase stay readable.

11 |

@Ripcraze

11 months ago

So much better, and even worse then one if-else is when I see PRs with a bunch of else-if conditions, typically very hard to follow and I've never found a time where it couldn't be made more readable by removing it.

4 |

@pinguincoder

9 months ago

The reason of clean code is to make the code more readable and maintainable for everyone in the team. I can understand your last approach with the expression but as we are coding in C# and not F# and those expression trees are not that commonly used by developers I would rather opt for the version before everything was cramped into one expression simply because I'm not alone in the Team and other developers have no clue about functional languages and will find that statement hard to read.

1 |

@luc9volts

11 months ago

Awesome. I always try to do that way

3 |

@Sindrijo

8 months ago

My preference is 2# and #3 together, meaning the guard/special case clauses stay like they are and the for loop is turned into a single expression. It's the same as #3 just not using nested ternary, terse code is more often misread than slightly more verbose code. Your formatting helps but what you really are doing is turning back it into a more procedural style. I would probably like to use #3 but I just know it will likely hurt readability for others reading the code. I try to reserve ternary for simple returns/assignments and I never nest them.

|

@metehanmutlu9187

11 months ago

Last version of code is the example of bad usage of ternary operator in my opinion. Not that readable and maintainable.

16 |

@zbaktube

8 months ago

Should you change the return type, or add a new guard clause before it overflows for a sufficiently big int
?

|

@pinkman_________________

11 months ago

Can you make a video about how primitive types and structures works together? As a student, I can't find suitable answer answered on that level of knowledge.

1 |

@yipyip454

11 months ago

but the single line expression is amost unreadable or debugable

1 |

@FlyBy2507

7 months ago

Now ask a junior programer to maintain this code...

|

@kaizer-777

11 months ago

Smaller is not necessarily better. I'd take the readability of the previous versions over the last one every time.

2 |

Go To Top