Start learning free

LESSON 76 OF 338 · CONDITIONAL STATEMENTS

Reducing If-Else to a Boolean Expression in Go

Practice Read the full explanation

We will learn a simple trick to improve our code quality: Reducing If-Else to Boolean Expressions.

Video Lesson:

STEP 1 OF 1
    Ctrl + Enter to check

    GO · CONDITIONAL STATEMENTS

    Reducing If Else To Boolean Expression in Go, explained

    Problem overview

    The function legalAge takes an integer age and returns a bool: true when the person is 18 or older, false otherwise. For 17 it returns false; for 18 and for 40 it returns true.

    The point of the lesson is a code-quality habit, a way of writing the same logic with less code. Many beginners write this function with an if and an else. This lesson shows that the if isn't needed.

    Approach: the comparison is already the answer

    The long version looks like this:

    func legalAge(age int) bool {
        if age >= 18 {
            return true
        } else {
            return false
        }
    }
    

    Read it carefully: when the condition is true, it returns true; when the condition is false, it returns false. The function returns exactly what the condition evaluates to. A comparison such as age >= 18 is an expression whose value is a bool, and a function can return any expression of the right type. So the whole body shrinks to one line.

    The same rule works for any condition. If you ever write if cond { return true } else { return false }, replace it with return cond. For the reverse case, returning false when the condition holds, return its negation: return !cond.

    Step by step

    1. Return the comparison. Write return age >= 18. The operator >= includes 18 itself, which is right because someone who is exactly 18 counts as of legal age. The expression has type bool, which matches the function's result type.

    The solution in Go

    func legalAge(age int) bool {
        return age >= 18
    }
    

    Details that matter in Go:

    • The result type decides what you can return. The signature says bool, and a comparison produces a bool, so no conversion is needed. Go has no truthy values, so returning the number age or a string wouldn't compile.
    • Precedence. In return age >= 18 the comparison is evaluated first and its result is what return sends back. No parentheses are required.
    • Short is easier to read. The one-line form is less code to scan and has fewer places for mistakes, such as returning the same value from both branches.
    • Combined conditions work the same way. A function can return age >= 13 && age <= 19 directly. Another lesson in this chapter does exactly that for a range of ages.

    Reusing the result

    A function that returns a bool can sit directly in the condition of another if, which is where the short form pays off:

    func isMinor(age int) bool {
        return !legalAge(age)
    }
    
    if legalAge(20) {
        fmt.Println("adult")
    }
    

    Here isMinor(17) is true and isMinor(18) is false, and the if prints adult. Writing return age < 18 gives identical answers, and for a single comparison that is the simpler choice. Both forms avoid the three-line if that only repeats the condition's own value. The habit applies beyond ages: whenever a function's job is to answer a yes-or-no question, look for the expression that already holds the answer, and return it as it is.

    Edge cases and common mistakes

    • The boundary. legalAge(18) must return true. Using > instead of >= would return false.
    • Comparing with true. Writing if legalAge(age) == true in calling code is the same redundancy in a different place. Use if legalAge(age).
    • Negative ages. The function doesn't validate its input, so legalAge(-5) returns false. For this lesson that is the expected result.
    • Writing a ternary. Go has no ? : operator. Returning the comparison is the idiomatic replacement for the boolean case, and an if/else is the replacement when the two results are not booleans.

    Testing

    • legalAge(17) returns false.
    • legalAge(18) returns true.
    • legalAge(40) returns true.
    • legalAge(0) returns false.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free