6

So I've started looking at ruby, and a lot of things look nice but I'm quite put off by implicit return statements. I understand making everything return self or nil by default but not the last value of a statement.

To me it looks horribly fragile (especially) if you are working with a method that doesn't plan to return something (especially a function which changes state/destructive method !) , other people may end up relying on a return that is not important to the purpose of the method and has a decent chance of changing.

What's the point of implicit return? Is there a way it makes some things way simpler?

Is it considered good practice to always have a return to prevent implicit returns?

Am I worrying to much about this?

P.S. Do people often use implicit return when they mean to return a certain thing from a method, doesn't this make it easier for other people in your group to break each others code? Sure , document everything and give meaningful names you might say, but people don't always do that in real life.

Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
  • 1
    Try it for a while... A lot of things appear wrong at first glance until you give it a chance. It's an idiom, you can be explicit with your returns if you feel otherwise. – Gishu Jun 11 '10 at 08:05
  • 2
    Thanks for asking this. I remembered it's something I hadn't mentioned in the ruby gotchas list. http://stackoverflow.com/questions/372652/what-are-the-ruby-gotchas-a-newbie-should-be-warned-about – Andrew Grimm Jun 11 '10 at 14:14

3 Answers3

5

Make sure not to add puts statements to the end of a method or a block unless you want to return nil.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
3

You're worrying too much. In my ~5yrs ruby practice I have'nt had a case when I misrelied on some return value.

And it makes methods to look really self-explaining and improves readability. I think that's very natural.

zed_0xff
  • 32,417
  • 7
  • 53
  • 72
  • 5
    But doesn't explicit return(or the lack of it in other language) help improve readability in other languages (you can tell at a glance where you are returning and if you're not returning anything/anything important)? – Roman A. Taycher Jun 11 '10 at 08:03
  • You still can do a explicit return. Also, tools like YARD allow statements about the return value (and thus whether you can rely on it). – Konstantin Haase Jun 11 '10 at 09:07
  • I understand that you can still do it, its just in many other languages if you don't see a return statement you know the function/method returns nothing, null or self. Also what is Yard? – Roman A. Taycher Jun 11 '10 at 10:15
  • 1
    I have seen bugs (not mine) caused by the absence of an explicit `return`. Besides the asymmetry with early returns, it's often [not obvious what will actually be returned](http://stackoverflow.com/questions/1064159/implicit-return-values-in-ruby). However, I have never seen this as a problem in LIPS. – cdunn2001 Jun 22 '13 at 02:37
0

Good question.

This is probably one of those ruby features that should be treated with TLC.. eh, TATFT.. I mean, strong testing :)

Also it's a good incentive for you to supply RDocs for your methods that will state clearly what the methods return.

alex.zherdev
  • 23,914
  • 8
  • 62
  • 56