DEV Community

Cover image for The pitfall of implicit returns

The pitfall of implicit returns

Nicolas Fränkel on March 21, 2024

Implicit returns are a feature in some languages. They have recently bitten me, so here's my opinion. Statements, expressions, and return...
Collapse
 
sethcalebweeks profile image
Caleb Weeks

Arguably, implicit code would mean adding the else statement. But I agree with your sentiment. I first encountered implicit returns in Elixir, and I'm still getting used to them.

Generally, I like using expressions over statements, but sometimes you can make assertions at the top of a block which saves a lot of nesting further down using early returns. There are other ways of solving this issue, and I haven't heavily weighed the pros and cons yet.

Collapse
 
jhelberg_63 profile image
Joost

Good and important article. You present definitions of expression and statement, in that order. The kotlin snippet shows examples, but then you present a statement as 1 and 2 as expression; that is confusing, as the definition uses the reverse order. You can better reverse the order of the definitions.

Collapse
 
alexmario74 profile image
Mario Santini

That's a perfect example on how a small piece of code can easy became very complex.