The eval return statement

Our politician statesman is a very busy fellow in The Great Sync. We have seen him grandly declaring the existence of genie variables. We can see him regularly directing the flow in If / Else conditions. He’s crowned Czar genies with the var keyword, imprisoned genies with the const keyword, opened brand new shiny telescopes on reference rocks to objects.

All of that that we can consider his day job.

But he has another, more sinister occupation 🧛. In function executions he transforms into the eval return statement.

Yes, eval and not evil - to make us remember the word evaluation.

By evaluate, we are referring to the entire process of executing and calculating a function or expression to obtain the final value.

Don't confuse it with the window.eval() global method.

In our model, we can see the statement all dressed up. He has a special tool that can grip an entire island, and throw it over the edge to end the function execution. As developers, we have the power to tell him exactly which value to return.

He is evil if we don't tell him what to return, he will throw an island of burning, molten lava over the edge - undefined.

The return statement affects how we structure functions

If you have ever written a ridiculously long and complicated function (who hasn't?), it's because you did not think about functions as values returning something.

When we think of a function, we should think RETURN, and not DO.

What should this function return? If it is doing things not related to what it needs to return, then that logic shouldn't be there!

By thinking like this, we end up writing smaller functions with a single responsibility. Take a look at the code examples alongside of well-structured functions based on return.