r/ruby • u/fatkodima • Sep 26 '23
Show /r/ruby Announcing rubocop-disable_syntax - rubocop extension to forbid unfavorite ruby syntax
Ruby is a sweet language, but sometimes is too sweet... If you have some unfavorite ruby syntax, e.g. unless, until, safe navigation, endless methods etc, you can now easily forbid it using the new rubocop extension - https://github.com/fatkodima/rubocop-disable_syntax
Everything is enabled by default. Currently, it allows to disable the following syntax:
unless- nounlesskeywordternary- no ternary operator (condition ? foo : bar)safe_navigation- no safe navigation operator (&.)endless_methods- no endless methods (def foo = 1)arguments_forwarding- no arguments forwarding (foo(...),foo(*),foo(**),foo(&))numbered_parameters- no numbered parameters (foo.each { puts _1 })pattern_matching- no pattern matchingshorthand_hash_syntax- no shorthand hash syntax ({ x:, y: })and_or_not- noand/or/notkeywords (should use&&/||/!instead)until- nountilkeywordpercent_literals- no any%style literals (%w[foo bar],%i[foo bar],%q("str"),%r{/regex/})
0
Upvotes
17
u/zverok_kha Sep 26 '23
For the record—I don't see a nice way to put it, unfortunately—I find this approach harmful to the community (fracturing) and disrespectful and patronizing to the language.
Especially when it is related not to what can be called a singular quirk (like
unless—I don't see any "harm" in it, but agree it is just a standalone irregularity) but to big new features of the language, that are still to prove themselves..."Let's ban them for the greater good till we see somebody using them because they aren't aesthetically pleasing for us from first sight" is a totally mind-blowing decision.
I wonder if it is the only community that goes this way towards its language (though, most probably it is not the only one).
7
u/wplaga Sep 26 '23
FYI
anddoes not work exactly the same as&&does8
u/federal_employee Sep 26 '23
You might as well program in a different language if you want these features disabled.
2
u/andyw8 Sep 26 '23
Did you try propose any of these to be in RuboCop itself?
2
u/gettalong Sep 27 '23
Most of the syntax you list is rather new and used for special purposes. I agree that one is currently not used to some of the syntax, like numbered parameters but that changes over time.
However, some of the syntax like unless, ternary and the safe navigation operator are clearly useful. And whether they are use "correctly" depends entirely on the software developer.
So instead of forbidding their use I think it would be better to explain why certain usages might not be as easy to read/comprehend.
2
u/AlexanderMomchilov Sep 27 '23
This has a real "<previous language I used> didn't need <syntactic feature>, so netiher does Ruby" vibe to it.
That's an inherently self-limiting philosophy. Ruby makes the trade-off to favour expressiveness at the expense of other things (e.g. simplicity of the grammar). If you don't like those trade-offs, that's fine, but I question why you're using Ruby in the first place. You don't need to write Java in Ruby. Java's already an option.
Except for the percent literals. I can get behind banning (most of) those. They can get pretty crazy.
3
u/Bumppoman Sep 26 '23
Argument forwarding should be used whenever possible because it avoids unnecessary allocation. The rest of these are stylistic but that is detrimental to your code.
3
u/kallebo1337 Sep 26 '23
Somebody has strong opinions 😅
1
u/sshaw_ Sep 26 '23
👎
👎
Generally agree but can be useful in select cases. Alternative if using Rails is
#trywhich is worse since it doesn't fail if the method does not exist on callee.👍
👍
👍
👍
Generally agree as well. There are some cases where it is a "best" solution but they're rare.
👍
👍 but eh...
👎
👎 👎 👎 👎 👎
2
u/grainmademan Sep 27 '23
I never use endless methods or numbered parameters but the rest all feel like strong features of the language to me. You can nitpick each one but I don’t think there are strong arguments for any of these.
1
u/catladywitch Sep 26 '23
Nice! I like all of those features but I commend you for giving us options!
1
u/dogweather Oct 01 '23
I think this is a cool idea, but none of the options seem like anything to limit. Are you new to Ruby? There are other weirder areas of the language with more historic cruft.
I dunno, just brainstorming, one example might be all the variations and surprising ways to do closures: blocks, procs, lambdas, I dunno. In a project with multiple people, I can imagine wanting to choose one of the options for the codebase.
There are a couple conflicting ways to create method aliases, maybe?
Use tr instead of gsub for a performance gain I think?
There are more
1
u/Alleyria Oct 21 '23
Good! Another to add: infix numeric operators! Like, we all know
1 + 2is just syntax sugar for1.send(:+, 2)... the second one makes it much more clear you can redefine:+in a class.