Hi,
Many thanks for the great feedback and for taking your time! I’ll try to
wrap-up the main points of the discussion first:
As everything’s an expression in Ruby, the last statement of a method
defines its return value; the favored implicit return. Alternatively, you
can explicitly return with the return statement, which is not the common
way in Ruby. This leaves it open how to return from a command-method
(CQS[1]), which should ideally return nil. A majority, at least in this
discussion, sticks to the implicit return even in command-methods, leaving
it up to the caller to ignore the more or less random return value. This
bears the risk of leakage of internal data and other unintended
consequences (dependencies, …). One suggestion, wich is like, is to
explicitly return (nil) from public command-methods. Returning self is
another topic, related to chaining methods.
The summary and my understanding is based on the following extract /
details from the discussion:
> Am 25.09.2016 um 18:43 schrieb Samuel Brandão <gb.samuel@gmail.com>:
>
> If you just want a method to return nil, just write nil as the last
expression, without the return keyword.
> Am 25.09.2016 um 19:08 schrieb Robert Klemme <shortcutter@googlemail.com > >:
>
> Returning nil instead of an empty String seems so much more reasonable
Good point(s)! So,
~~~
def foo_cmd
# …
nil
end
~~~
is kind of the „explicit-implicit“ return for command methods; from my
point of view this makes the command nature of a method very clear /
explicit.
> Am 25.09.2016 um 19:08 schrieb Robert Klemme <shortcutter@googlemail.com > >:
>
> Where did you see the empty String return?
Thanks for pointing me in that direction; I looked it up again and have to
correct myself: it was a query method yielding strings (as enums) and
returning the empty string as the last one to indicate the end of the enum.
> Am 25.09.2016 um 18:43 schrieb Samuel Brandão <gb.samuel@gmail.com>:
>
> it is up to the caller to not rely on the return value
Which seems to be consensus:
> Am 25.09.2016 um 21:30 schrieb Marvin Gülker <m-guelker@phoenixmail.de>:
>
> I do think that the community does not really care
I think of being explicit about the nature of a method at least as kind of
a courtesy to the caller / other programmers. Therefore, I like Robert’s
approach of at least being explicit in public methods as a reasonable
tradeoff, esp. as there’s obviously more to it than courtesy:
> Am 25.09.2016 um 19:08 schrieb Robert Klemme <shortcutter@googlemail.com > >:
>
> For public methods I usually make sure no internal data is leaked
because this would allow accidental or intentional manipulation of instance
state which likely violates the class invariant.
The risk is that someone might tie herself to this undocumented and
implicit part of the public API of a class.
> Am 25.09.2016 um 19:08 schrieb Robert Klemme <shortcutter@googlemail.com > >:
>
> the focus in Ruby is generally on pragmatic reasonable solutions and
less on strictly adhering some kind of rule system
I totally agree and wouldn’t like Ruby as much as I do if it were
differently! My intention was to get a feeling of how other developers
handle this much appreciated "freedom" offered by the language.
> Am 25.09.2016 um 21:11 schrieb Marvin Gülker <m-guelker@phoenixmail.de>:
>
> Returning `self' allows you to chain method calls. This is for example
fairly nice with operators; […]
Many thanks for your really helpful example; I’ll have to play around with
it a little bit but I think I’ve got the point of returning self now. It is
not meant as a replacement for nil for command methods, but THE return
value of this kind of (query) methods.
> Am 25.09.2016 um 21:30 schrieb Marvin Gülker <m-guelker@phoenixmail.de>:
>
> if a method has no documented return value, it is a „command method“
Hm, I’m not sure I like that one…relying on documentation? I can see your
point here, but documentation is neither a „first-class citizen“ of any
programming language I know of, nor of most of the developers I’m aware of,
including me, so it boils down to luck, which is never a good principle.
Which kind of documentation do you have in mind here?
> Am 25.09.2016 um 21:30 schrieb Marvin Gülker <m-guelker@phoenixmail.de>:
>
> I would welcome more discussions like these as well as they have become
way too rare
Good to know; I was not sure whether to start this discussion here or not
as it definitely is a slightly more general topic.
So again, a big thanks to the contributors; your input and time is really
appreciated and not taken for granted!
Cheers,
Michael
[1] Command Query Separation
Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>