Uploaded December 2023 | Updated September 2026, 3 weeks ago
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 ⺠patreon.com/ZoranHorvat
â ð Subscribe ⺠youtube.com/channel/UCxsWfh8LCcn55mFB6zGBT1g?sub_confirmation=1
â 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 ⺠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 ⺠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
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 ⺠patreon.com/ZoranHorvat
â ð Subscribe ⺠youtube.com/channel/UCxsWfh8LCcn55mFB6zGBT1g?sub_confirmation=1
â 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 ⺠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 ⺠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










