Explain Empty Returns Like I'm Five
Christian Vasquez

Christian Vasquez @chrisvasqm

About: Un simple mortal

Location:
Santo Domingo, Dominican Republic
Joined:
Feb 22, 2017

Explain Empty Returns Like I'm Five

Publish Date: Oct 12 '18
29 17

This kind of code can be confusing for beginners when they start coding:

function doSomething() {
    if (somethingElseHappens())
        return;

    // ...
}
Enter fullscreen mode Exit fullscreen mode

How would you explain this in a real life scenario that could tell how/why this is useful?

Comments 17 total

  • Casey Brooks
    Casey BrooksOct 12, 2018

    It's really nice in validation functions, where certain conditions must be met before proceeding. By returning early (maybe with a log about why), you keep the validation logic entirely flat. The alternative is making a deeply nested set of checks that are even more difficult to understand, since deep nesting makes it more difficult to know which lines of code are related

  • Christian Vasquez
    Christian VasquezOct 12, 2018

    Hey RedSpy,

    Thanks for taking your time to comment. But the #explainlikeimfive hashtag is intended to be sort of like a "challenge" that involves explaining concepts with day to day situations so that an actual 5 year old can understand.

    This may make things harder for those who reply but I've seen some really creative ideas here and there.

    You can take these as example from Explain Dependency Injection Like I'm Five

    Traditional: Picking what to wear when you get up in the morning.

    Dependency Injection: Asking a pants, t-shirt and an hat from a stylist, and he makes sure you look lit AF.

    "Dependency Injection" is a fancy way of saying that you have to ask someone else, like Mummy or Daddy, to give you anything you want to play with. You can't make the toy yourself, you need Mummy and Daddy to get it for you.

    Sometimes, you don't know what toy you are going to get, like on your birthday, but all you need to know is that it will be a fun toy you can play with when you open the present.

    • Isaac Lyman
      Isaac LymanOct 12, 2018

      If I may add my two cents: all kinds of explanations are welcome on #explainlikeimfive. If we take the hashtag literally, we can't really get to the heart of a lot of technical topics. I mean, when I was five I was still potty training (sad, I know). I think simple--but perhaps not five-year-old simple--explanations are super valuable and this is a great place for them.

      • Christian Vasquez
        Christian VasquezOct 12, 2018

        Hey Isaac,

        Yeah, you are right. I apologize if the way I expressed myself sounded like it was a mandatory way of doing it.

  • Sung M. Kim
    Sung M. KimOct 12, 2018

    You decided to go shopping for a gift(doSomething).
    As you arrived at a train station, you got a push notification that all trains are canceled due to hurricane (somethingElseHappens()).
    So you returned home without any gifts (return).
    Or else you could've waited for hours for a train that will never show up.

    • Jerod Santo
      Jerod SantoOct 12, 2018

      That was a super useful push notification! ✨

    • Joe Zack
      Joe ZackOct 22, 2018

      Great example!

      • Sung M. Kim
        Sung M. KimOct 23, 2018

        😎 Thanks Joe~ 👊

      • Steven Bruno
        Steven BrunoOct 24, 2018

        Woah, I listen to your podcast all the time - never thought I'd see your name pop up in the wild!

  • Christian Vasquez
    Christian VasquezOct 12, 2018

    Hahaha, don't worry 😂. Feel free to add the new one.

  • Dustin King
    Dustin KingOct 12, 2018

    I'd add more to the function:

    function doSomething() {
        if (somethingElseHappens())
            return;
    
        stuff_i_dont_want_to_happen_if_somethingElseHappens_returned_true();
    }
    

    Example:

    function doCookieCheck() {
        if(cookiesAreAuthorized()){
            return;
        }
    
        popUpAnnoyingCookieAuthorizationBanner();
    }
    
    
  • IceTruckCol
    IceTruckColOct 12, 2018

    Some functions are designed to return a value while others have a job to do that doesn't require anything to be returned. Return means exit the function either way but, if the design of the function expects a value to be returned, it must follow return like return 23;

    When you start coding, you usually put returns at the end of functions after the work has been done. But after a while returns are placed inside ifs and loops and you usually end up coding multiple returns depending on the state of the application.

  • Owen Crabtree
    Owen CrabtreeOct 12, 2018

    Its like when you go to the kitchen for cookies but there are no cookies so you return with nothing

Add comment