From: Mike Wyer Date: 2003-02-08T02:19:45+09:00 Subject: Re: Ruby "Procedures" & their return values On Sat, 8 Feb 2003, Austin Ziegler wrote: > [Please note that I am way behind on my ruby-talk reading. If you need a quick response, email me directly.] > > In the thread on return values from assertions, I have seen a couple of > people express that they prefer functions/methods without meaningful > return values to return nil. Given that Ruby is a dynamically typed, > expression-oriented language, this makes no sense to me. > > Even if the return value of a method is meaningless and not guaranteed > to be something useful (a documentation matter, IMO), that doesn't mean > that it doesn't provide value. In particular, since nil is a Boolean > false value, returning nil from these methods means that the method > cannot be used in a test, as it is always "false". > If the method fails, it should raise an exception. In which case the return value is worthless- if it would be false, you'd never know because the exception would already have been triggered. Testing a meaningless output should be discouraged by always having the method return nil. It's painful for C developers to trust the exception mechanism, but it makes for much cleaner code. begin do_thing do_other_thing foo rescue BadThing warn_bad_thing end versus begin if do_thing if do_other_thing foo else warn_bad_thing endif else warn_bad_thing endif end Sure, there are ways of cleaning up this construction, but the principle remains. Don't encourage bad habits by ignoring them or even supporting them. Return nil and you can help people do the right thing. Cheers, Mike -- Mike Wyer www.wyer.org/mike 07974 254007 "I want to die peacefully in my sleep like my Grandfather... ... not screaming and yelling like the passengers in his car"