Conversation
|
That's my first impression too and I voiced my concerns at the time. Since this was announced I've been realizing that there are indeed many single line methods. I might experiment in my personal style for simple methods, say with at most one argument. In short, maybe we should give it time and see how good or bad it can be? |
|
I'm in favour of the guideline, even though I assume some people like the new syntax. I'm in no rush to merge this, though. |
|
I also look forward to feedback from community :-) |
|
I've dropped a tweet on topic https://twitter.com/bbatsov/status/1341653482592464896 and referred to this discussion. I guess we'll get some feedback soon. |
|
I vote to not recommend them. As said they are just another form of one liners and we already have good cops for them |
|
Btw, when it comes to rationale - my biggest gripe with them is that they introduce (yet another inconsistency) in Ruby's syntax. |
Fully agree here. Endless methods add nothing but inconsistency. |
|
And a couple of syntax edge cases that annoy me: # syntax error
def foo = bar
# correct
def foo() = bar
# syntax error
def foo() = bar baz
# correct
def foo() = bar(baz)The thing I dislike most in any language is having "special cases" and this syntax is a case study in adding special cases. :-) |
This smells like JavaScript for me.....not Ruby |
|
IMO, the syntax should be used only when it is in one line and the body has a simple expression without side effect. |
@mame Thanks for the great feedback! I guess we can rework the guideline to be something in line with your suggestion, as it certainly makes sense to me. |
|
I have one question then, more out of curiosity. Then
|
I think that sometimes a class design requires it to be a method. The most trivial case is, maybe, a callback or event hook.
Yes, |
|
Thanks. Great example with the callbacks, that's definitely a valid use-case in Rails that could warrant the usage of these methods to keep the controllers thin and support the required behavior! I get the second argument, but I personally feel it's not strong enough to convince me of the usage for that use-case. |
FYI there's an issue opened to address that: https://bugs.ruby-lang.org/issues/17398 |
Jokes aside, I'd love to have a configurable cop to ban or force consistent usage. 🙂 I would like to not make it seem as if RuboCop is "taking a stance" on which one. There is enough energy spent on issues and PRs opened to shoehorn someone's personal preference as the default because they mistake the default for The Way™. 😅 |
|
FYI a new |
|
@dvandersluis We should update this PR to reflect this. |
fbd1fad to
154040b
Compare
|
@bbatsov agreed! I've updated the wording now. |
|
@dvandersluis I think it'd be better if you used the examples by @mame. |
154040b to
1528220
Compare
|
I've copied them verbatim now so let me know if you want me to change anything 😁 |
|
I think we're good for now. Thanks! |
|
Edit: I image that single-line methods mean fitting in one line, not with a single line body... |
|
@marcandre yeah, it follows the section on single-line methods (which is the previous section of the style guide). It's meant to mean methods on a single line, not with a single line body (and maybe that needs to be clarified). FWIW I'm still on team no-endless methods 😆 |
Endless methods are about to be added by ruby 3.0 but IMO they are just another form of single-line methods (which we have a rule for) and make it harder to read the structure of a ruby file.After discussion it was decided to have the style guide allow endless methods, but only those that are a single line.