Sometimes when you return early it could have a severe impact on relationships or leads you to jump in the path of a moving train but this time we're talking about code. Have you ever returned early? Just asking 😬

The if-statement

An if-statement is used in all programming languages. It's used to check for a condition and when evaluates to true, it does what you'd expect. Ever had two plans, "a" and "b"? When using an if-statement, you'd have a plan "a" and/or a plan "b". You could avoid plan "b" by not using an if-statement where possible. In some cases, we never implement a "plan b" and that's bad, bad in a strictly-typed code base. Take one, for example, written in Swift:

fileprivate func checkForAdult(human: Object) -> Bool {
 if human.age >= 21 {
   return true
 }
}

Swift would complain saying "hey, if I'm under the age of 21, should I return true or false?", as you've declared you should return a Bool (true/false). In this case you'd return false outside of the if-statement.

Return late is dangerous

Without getting in code, think of this situation; we have a restricted area where inside is only for the eyes of an adult. There is a guard (a bouncer) at the door and that guard's responsibility is to hinder a child from entering an adult-only building. Instead of the guard being at the door, he/she is inside and at the back of the establishment then walks around to check for anyone that is not at the required age. By the time an underage person enters, the damage is done. That child has now seen things they should not have seen. The establishment has now lost its credibility with shame and disgrace. By having a guard at the door, that child would have been sent back home (the return).

Code

We'll now simulate this scenario in code.

// The damage
function beNaughty(adult) {  
    // Simulating a fake function
    drinkOnHungryStomach(adult)
    
    if (adult === isChild()) return
    
    // The damage has already done
    
    doEvilThings(adult)
}

Not only that the child sees someone drinking uncontrollably, that child has evil recordings in their minds. Peer pressure doubles and the mind to try will result in an experience gained. After, just before doing something very evil, we do our checks for a child. This is considered too late. The correct way:

// The damage
function beNaughty(adult) {
    // Send child home to their parents
    if (adult === isChild()) return
    
    // Simulating a fake function
    drinkOnHungryStomach(adult)
        
    doEvilThings(adult)
}

Better, right? While this would work, we could make it a little better. Sadly our little one got past our guard and saw a glimpse of the naughty activities inside. Let's do this better:

function beNaught(adult) {/* evil things */}

// Checking for value to be true. No plan 'b' here
if (adult === isAdult()) {
    beNaught(adult)
}

Enter TypeScript

TypeScript can be used to properly describe an adult property, or state the acceptable age in which an adult should be before entering an adult-only building. For this, we'll be using interfaces as OOP.

class Human {
  fullName: string;
  constructor(public firstName: string, public isAdult: boolean) {
    // do normal class constructor stuff
  }
}

interface Person {
  firstName: string;
  isAdult: boolean;
}

function checkForAdult(person : Person) {
    return person.isAdult
}

let littleOne = new Human("Claire", false);

// This would return false
checkForAdult(littleOne)

How do we know we should use false as the second param of our interface object? Somewhere in our code, our guard that's at the door, decides whether or not to use true or false 😉