This kind of code can be confusing for beginners when they start coding:
function doSomething() {
if (somethingElseHappens())
return;
// ...
}
How would you explain this in a real life scenario that could tell how/why this is useful?
Top comments (16)
That was a super useful push notification! ✨
I should've used FEMA Alert as an example 😄
Great example!
Woah, I listen to your podcast all the time - never thought I'd see your name pop up in the wild!
😎 Thanks Joe~ 👊
Its like when you go to the kitchen for cookies but there are no cookies so you return with nothing
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.
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.
Hey Isaac,
Yeah, you are right. I apologize if the way I expressed myself sounded like it was a mandatory way of doing it.
I'd add more to the function:
Example:
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
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.
Hahaha, don't worry 😂. Feel free to add the new one.