DEV Community

Cover image for Conditionals
Winston Puckett
Winston Puckett

Posted on • Updated on

Conditionals

"If the milk carton is empty, add milk to the grocery list."

The example above is a conditional. We only want to add milk to the list if we need to. Programming languages have different ways of expressing conditionals. The most common ways of expressing conditions are if statements, switch statements, and ternary operators. For the most part, these can be interchangeable, but readability increases dramatically when you understand how they are going to be read next time someone is in the code.

When you want you code to read like a sentence (If)

"If" statements read exceedingly well when you have few cases and need to input more than one line of code for each case. When you have multiple ifs, you can use "else if" and "else" to specify a set of conditions where only one should be executed. An example of that lies below.

// written in JavaScript
if (milkCarton.percentFilled > 80) {
    hand.fillBowl(Cereal);
    hand.fillBowl(milkCarton);
    hand.putAway(milkCarton);
}
else if (milkCarton.percentFilled > 30) {
    hand.fillBowl(Cereal);
    hand.fillBowl(milkCarton);
    hand.putAway(milkCarton);
    groceryList.Add(Milk);
}
else {
    groceryList.Add(Milk);
    hand.ThrowAway(Carton)
}
Enter fullscreen mode Exit fullscreen mode

Languages vary slightly in syntax. Your language may not use parentheses or semi-colons, but it almost always looks something like the code above.

When you want your code to read like a table of contents (Switch)

When you have a lot of cases and each case requires only one line, expressing that in an if/else series can be tedious and hard to read. Using a switch statement is often more readable because it wraps every case in an enclosing block. You should also consider a switch statement if you're cases revolve around what "type" an object is or which value in an enum is supplied. Switch statements use an outer variable and pass that to each case, which makes the finite and singular natural of types and enums align perfectly to switch statements.

// Written in C#.
var holiday = HolidayEnum.OppositeDay;
switch(holiday)
{
    case HolidayEnum.MemorialDay:
        RememberTheFallen();
    case HolidayEnum.OppositeDay:
        DoEverythingOpposite();
    case HolidayEnum.MyBirthday:
        HoldBirthdayParty();
}
Enter fullscreen mode Exit fullscreen mode

Switch and if statements can often be converted into each other easily. The thing to remember is that switch statements look the best when there are lots of one line cases and if statements look the best where there are few multiline cases.

When you're just trying to assign something to a variable (Ternary)

Ternary operators are often overlooked as a conditional. Experienced programmers use them without thinking and beginner programmers use if statements more often.

// Written in C#.
var currentScore = 0;
currentScore += answeredCorrectly ? 10 : 0;
Enter fullscreen mode Exit fullscreen mode

In the code above, we want to add 10 to the current score if the boolean answeredCorrectly is true. Expressing this as an if statement would add unnecessary boilerplate to the code. Of course, ternary expressions are often hard to read for the first 20 times you see them.

Where to go next

We need to write readable code. A big portion of that is how to express your conditional statements. Next, research cyclomatic complexity and unconditional code. These teach not when to use a particular syntax, but how to reduce the number of conditionals you have to write.

Top comments (0)