Skip to content

Logical Flow

Kaisinel edited this page Sep 21, 2020 · 4 revisions

Logical Flow

Intro

In so many cases, code is not linear. We have conditions, which determine which path our code takes. Such code multi-pathing is done using if statements.

Syntax

If statement starts with if and then in brackets () we place a logical condition. Like so:

if(condition)
{
  // do something if condition is true
}

or

if(condition)
  // do something one liner

The scope ({}) is optional for one-liner statement, however it is recommended that you conventionally add the brackets. It's crucial that we do not miss the if statement branching logic and the extra brackets help even when they are not semantically needed.

Condition is anything that evaluates to true or false. For example: 5 > 4 will return true. 4==0 will return false. It's mostly comparisons, such as:

  • equal ==
  • less than or equal to <=
  • less than <
  • more than >
  • more than or equal to >=

You can negate any condition using !. For example: !true evaluates to false. != reads as not equal. !(a > b) not a more than be

Please note that you don't have to explicitly say that something is true or false in your if statement if you are working with a bool value. For example: This requires explicit comparison

if(x == y)
{
  // do something
}

but this does not

bool x = false;
if(x == true)
{
  // do something
}

and so it can be simplified to just

bool x = false;
if(x)
{
  // do something
}

If statements can be chained using else if or else keyword:

if(condition1)
{
  // do 1
}
else if(condition2)
{
  // do 2
}
else
{
  // do 3
}

Input validation if statements and return

If statements are especially useful when we need to validate input. We aim to do the validation first and return as soon as we can because that will allow us to care about the actual logic of a function without mixing it with mere validation. For example, instead of this:

public static void Foo(string message)
{
  if(!string.IsNullOrEmpty(message))
  {
    if(another_condition)
    {
      // do something
      // bad! 3 levels deep!
    }
  }
}

do this:

public static void Foo(string message)
{
  if(string.IsNullOrEmpty(message)) return;
  if(!another_condition) return;
  // do something
  // good!- all is one level deep!
}

The benefit might not be evident right away, but what just happened is that we de-nested the first version of the function, making it flatter and easier to read. The magic behind this is mostly due to an if statement that allows immediately ending the function and returning to the caller context.

Switch statements

Switch statement is a sibling of an if statement. It does the same, but it's a specialized case of an if statement only applies to matching values or patterns nad not just bool.

For example, it can be handy to print a day of the week based an integer value.

int a = 5;
if (a == 1)
  Console.WriteLine("Monday");
else if (a == 2)
  Console.WriteLine("Tuesday");
else if (a == 3)
  Console.WriteLine("Wednesday");
else if (a == 4)
  Console.WriteLine("Thursday");
else if (a == 5)
  Console.WriteLine("Friday");
else if (a == 6)
  Console.WriteLine("Saturday");
else if (a == 7)
  Console.WriteLine("Sunday");
else
  Console.WriteLine("Huh?");

Using switch statements can highlight the fact that all we care about is the value of that integer, like so:

int a = 5;
switch(a)
{
  case 1:
    Console.WriteLine("Monday");
    break;
  case 2:
    Console.WriteLine("Tuesday");
    break;
  case 3:
    Console.WriteLine("Wednesday");
    break;
  case 4:
    Console.WriteLine("Thursday");
    break;
  case 5:
    Console.WriteLine("Friday");
    break;
  case 6:
    Console.WriteLine("Saturday");
    break;
  case 7:
    Console.WriteLine("Sunday");
    break;
  default:
    Console.WriteLine("Huh?");
    break;
}

This prints "Friday" to the console.

We use switch statement when all the branching logic depends on different values of a variable. Switch statement increases readability. However, in the end, the best kind of logic is fluent logic, when if or switch statement is not needed altogether.

Clone this wiki locally