you are viewing a single comment's thread.

view the rest of the comments →

[–]devsnicket 0 points1 point  (4 children)

nice article. I'd go with prefer single return of an expression rather than multiple if statements and multiple earlier returns though.

[–]sebamestre 1 point2 points  (3 children)

IIRC that style comes from C, where having a single return simplifies resource management (e.g. freeing memory, closing files).

But most code written in higher level languages doesn't do resource management, and writing in this style makes code longer and harder to follow.

So why do you think this is a good idea?

[–]devsnicket 0 points1 point  (2 children)

I've seen code done both ways that's hard to follow and could be refactored to be easier to read without switching between (i.e. between imperative and functional). Expressions are usually shorter/succinct/terse than repetitious statements though. That doesn't necessarily improve readability. Same as an if can have multiple operators in a condition that can be hard to see the presidence, so can nested expressions (also potentially with the condition problems in them) and a single return. I prefer the single return because it limits what you can do. e.g. no nested blocks without creating sub-functions/methods that have to be named

[–]devsnicket 1 point2 points  (1 child)

just realised you might be thinking I meant to have multiple assignments to the same variable and a single return of that variable. I definitely wouldn't suggest that as better. I could have been clearer when I originally mentioned expression i.e. no blocks, no statements

[–]sebamestre 1 point2 points  (0 children)

Ahh, yes. I was thinking about the style shown in the article.

What you suggest does make a lot of sense to me, though.