2 ms·
I don't think "one line" should get associated with endless methods. I really love endless methods for any situation where the first statement in my method is a
by Lammy 3y ago
I don't think "one line" should get associated with endless methods. I really love endless methods for any situation where the first statement in my method is also intended to be the return value.
It lets me avoid my most hated pattern, where you instantiate a thing with a temporary variable, do some stuff to it, and then return the instantiated object with a hanging call to the temporary variable. I've always found it very ugly and also fragile if the order of operations gets mixed up by later edits. This pattern:
def do_the_thing(with_me)
thing = MyThing.new
thing.do_something_that_does_not_return_self(with_me)
thing
end
I also love to use it to avoid "double ends" for methods consisting entirely of control-flow statements:
def do_the_thing(with_me)
case with_me
when rofl then ...
when lol then ...
when lmao then ...
else ...
end
end
With endless methods I would write these two examples as:
def do_the_thing(with_me) = MyThing.new.tap {
_1.do_something_that_doesnt_return_self(with_me)
}
def do_the_thing(with_me) = case with_me
when rofl then ...
when lol then ...
when lmao then ...
else ...
end