Created
January 23, 2024 10:12
-
-
Save emmanuel-pangan/89a41ecff722a54631b0497eff5ac412 to your computer and use it in GitHub Desktop.
How to Avoid Multiple Nested If-Else Statements Using Guard Clause
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Programs | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
UserLogIn(true, true, true); | |
} | |
private static void UserLogIn(bool hasUsername, bool hasPassword, bool isAdmin) | |
{ | |
if (!hasUsername) | |
{ | |
Console.WriteLine("No Username Found."); | |
return; | |
} | |
Console.WriteLine("Username"); | |
if (!hasPassword) | |
{ | |
Console.WriteLine("No Password Found."); | |
return; | |
} | |
Console.WriteLine("Password"); | |
if (!isAdmin) | |
{ | |
GoToWelcomePage(); | |
return; | |
} | |
GoToAdminPage(); | |
} | |
private static void GoToWelcomePage() | |
{ | |
Console.WriteLine("Welcome Page"); | |
} | |
private static void GoToAdminPage() | |
{ | |
Console.WriteLine("Admin Page"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment