I'm with Ben. As you said, "It is a method that is supposed to be called
only once" so I'd raise an exception to chastise the caller, who of course
can then take steps to dance around the exception. But at least that way,
callers can't be oblivious to that they did something unexpected.

It's old-school Design by Contract, but then, I'm an old schooler :-)


On 27 April 2013 18:39, Sebastian Porto <[email protected]> wrote:

> Thanks Ben
>
> I am after the side effect here. It is a method that is supposed to be
> called only once. Subsequent calls will do nothing. Returning false seem
> like the cleanest option. But that is not enough feedback for the user, as
> it might return false because it was already called or because some
> condition failed. I am just trying to find the most user friendly pattern
> for this.
>
> Thanks
> Sebastian
>
> On Saturday, 27 April 2013 13:09:45 UTC+10, ben_h wrote:
>>
>> It depends on whether you're calling the method for its value, or for a
>> side-effect.
>>
>> If you want the value, then I'd make repeated calls just return the
>> value, so the method is memoized (assuming the initial call is expensive).
>>
>> If it's side-effecting, my feeling is that the method should always do
>> its job whenit's called. If that causes an error, then you'll get a
>> legitimate exception, which should be handled by the caller.
>>
>> One exception is a method that explicitly should only run once, like
>> ruby's #require. In that case, I think returning false (and true for the
>> initial call that does work) makes sense; that's how #require behaves.
>>
>> - Ben
>>
>>
>> Sent from my iPhone
>>
>> On 26/04/2013, at 3:05 PM, Sebastian Porto <[email protected]> wrote:
>>
>> (Sorry if this ends as a double post, as I send this using email, but for
>> some reason it doesn't show in the group. So I am posting it again directly
>> on the group page.)
>>
>> Hi All
>>
>> I'm building a gem and trying to decide what is the best way to provide
>> return values.
>>
>> Let's say that my gem add the do_something method to user:
>>
>>     user.do_something
>>
>> But if that something is already done I want to provide meaningful
>> feedback, I can't decide what is a best practice here or what is better,
>> these is what I can think of:
>>
>> Option 1. return false
>>
>>     res = user .do_something
>>     res //=> false
>>     user.errors //=> ['already done'] information about the errors, like
>> active record
>>
>> Option 2. raise an exception (seems a little drastic)
>>
>>     begin
>>         res = user .do_something
>>     rescue MyGem::AlreadyDoneException => e
>>
>>     end
>>
>> Option 3. return an error code
>>
>>     res = user.do_something
>>     res //=> 3 (meaning already done)
>>
>> Option 4. return an object
>>
>>     res = user.do_something
>>     res.success //=> false
>>     res.messages //=> ['already done']
>>
>> Any recommendations on what is a best practice here?
>>
>> Thanks,
>> Sebastian
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby or Rails Oceania" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to rails-oceani...@**googlegroups.com.
>> To post to this group, send email to [email protected]**.
>> Visit this group at 
>> http://groups.google.com/**group/rails-oceania?hl=en<http://groups.google.com/group/rails-oceania?hl=en>
>> .
>> For more options, visit 
>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>> .
>>
>>
>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/rails-oceania?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>



-- 


*Mark Ratjens*
*Co-founder, Habanero Software*
*
*
*
*
Sydney, Australia
[email protected]
@MarkRatjens <[email protected]>
www.habanerohq.com <http://habanerohq.com>
+61 414 159 357

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rails-oceania?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to