DEV Community

Discussion on: How I improved my code by returning early, returning often!

Collapse
 
iquardt profile image
Iven Marquardt • Edited

Propagating NEVER use else doesn't make much sense for a post tagged with #functional, because it is synonymous with NEVER use FP. FP is about expressions that must evaluate to values, which obviously doesn't work if you drop the else branch. OOP on the other hand is about statements that evaluate to nothing but perform side effects. So expressions stand for pure values whereas statements stand for impure effects.

Regarding your example:

func main() {
    data, err := loadData()

    result, err := someCalculation(data)

    return result, err
}
Enter fullscreen mode Exit fullscreen mode

It is not bad because you don't return early but because you rely on side effects in the first place. I think you made a really important observation but drew the wrong conclusion. Your example is simple and contrieved. Think about a huge program with side effects. I don't think that exiting early will save you in the long term but abandoning side effects will.

Collapse
 
jordanfinners profile image
Jordan Finneran

Thanks for the comment.
One of the way's I like to think about avoiding side effects is this returning thought process :)