DEV Community

Explain Empty Returns Like I'm Five

Christian Vasquez on October 12, 2018

This kind of code can be confusing for beginners when they start coding: function doSomething() { if (somethingElseHappens()) return...
Collapse
 
dance2die profile image
Sung M. Kim

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.

Collapse
 
jerodsanto profile image
Jerod Santo

That was a super useful push notification! ✨

Collapse
 
dance2die profile image
Sung M. Kim

I should've used FEMA Alert as an example 😄

Collapse
 
thejoezack profile image
Joe Zack

Great example!

Collapse
 
stevenbruno profile image
Steven Bruno

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

Collapse
 
dance2die profile image
Sung M. Kim • Edited

😎 Thanks Joe~ 👊

Collapse
 
owencrabtree profile image
Owen Crabtree

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

Collapse
 
chrisvasqm profile image
Christian Vasquez • Edited

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.

Collapse
 
isaacdlyman profile image
Isaac Lyman • Edited

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.

Thread Thread
 
chrisvasqm profile image
Christian Vasquez

Hey Isaac,

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

Collapse
 
cathodion profile image
Dustin King

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();
}

Collapse
 
cjbrooks12 profile image
Casey Brooks

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

Collapse
 
icetruckcol profile image
IceTruckCol

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.

 
chrisvasqm profile image
Christian Vasquez

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