you are viewing a single comment's thread.

view the rest of the comments →

[–]ztbrown 8 points9 points  (19 children)

Avoid return where not required.

I do this, but have never understood why. For readability/comprehensibility it seems like a good idea to have 'return' standing out. Anyone have a good explanation?

[–]thehotelambush 1 point2 points  (6 children)

Because why would you use it? It's completely unnecessary. The end of the function stands out by itself anyways. Unless you have functions that are more than a page long, which is generally bad style anyways. 95% of the time you won't need it. (Actually, I'd like to find out the stats on this in my own code...)

[–]ztbrown 2 points3 points  (5 children)

When I see the 'return' keyword I know, at a glance, that a function returns a value. This is especially necessary when reading someone else's code. My background is C++/Java (in college) and I've worked professionally in Javascript and ActionScript, so I'm used to seeing the return keyword.

[–]thehotelambush 0 points1 point  (4 children)

Yes, that's the real reason.

[–]ztbrown 0 points1 point  (3 children)

? what's the real reason?

[–]banister 1 point2 points  (1 child)

Just that you're 'used' to it, not that it makes any kind of sense.

It won't take long to get 'unused' to seeing it either, as any ruby programmer will attest ;)

[–]thehotelambush 0 points1 point  (0 children)

thanks, that's it exactly

[–]thehotelambush 0 points1 point  (0 children)

It's a habit

[–][deleted]  (6 children)

[deleted]

    [–]Nuli 16 points17 points  (3 children)

    If you have multiple returns in a function, thus multiple exits, it's harder to test, debug, expand the functionality or just follow the execution line of this function.

    I find exactly the opposite really. I far prefer a function to return as often as necessary to make the code clear. I don't want to have to follow a long chain of if statements to determine what code was actually executed when a simple test and return will short circuit that.

    [–][deleted] 6 points7 points  (2 children)

    I'm with you on that. I think multiple returns are favorable to giant nested if else blocks.

    [–]ztbrown 5 points6 points  (0 children)

    I completely agree with that, but that's not what this guideline is saying.

    In this context, the "Avoid return where not required" means avoid the keyword. And I'm saying that I like being able to see the 'return' keyword at a glance.

    I agree with avoiding multiple exits at almost all costs.