Skip to content

Instantly share code, notes, and snippets.

@emmanuel-pangan
Created January 23, 2024 10:12
Show Gist options
  • Save emmanuel-pangan/89a41ecff722a54631b0497eff5ac412 to your computer and use it in GitHub Desktop.
Save emmanuel-pangan/89a41ecff722a54631b0497eff5ac412 to your computer and use it in GitHub Desktop.
How to Avoid Multiple Nested If-Else Statements Using Guard Clause
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