-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefix unused variables and arguments with _
#335
Conversation
Fwiw, we typically use |
What's the benefit of following this convention?
|
FWIW, this is in the Ruby specific section. One situation I can think of: I'm not saying ^ is a good reason, but it does provide additional context. |
Sorry, I wasn't clear: what I meant was, I'm worried about taking a naming convention that's already going to be familiar to developers who know Swift/JavaScript/Python as meaning one thing, and using it to mean something different in Ruby. |
I have always thought that what Hound (or, rather, Rubocop) recommended to resolve an unused variable was a little unconventional. That said, I haven't had any issues with it. A case in which I think it's useful is when overriding a method that takes an argument, but you never use that argument. The underscore, I think, is a good way of communicating that the variable is actually never used. |
Rubocop and Syntastic recommend this because it's what Ruby recommends. Running Ruby with warnings will complain about unreferenced variables, but not if you prefix with an underscore, because that's a Ruby convention. |
Haskell uses an underscore to denote unused arguments when pattern matching. It's pretty common to use a plain underscore to denote an unused block argument in Ruby. |
I didn't know this, and before Hound started recommending it I'd never seen it in practice. Since it's the convention the interpreter wants us to follow, I'm fine with adding it to the guide. |
I'm fine with it, but, yes, I had never seen this before Hound recommended it. |
Underscore in Ruby has always meant that "this variable doesn't matter". I honestly don't care what a That said, the only time I've ever actually used it is in tests where I want something labelled but I also don't want to use it. This keeps syntastic (and Hound) from giving a warning. I'm fine with not adding this as a guide since it's just as likely that the variable shouldn't have a name/exist if we don't intend to use it. But I'm fine if we do, too, since this is what Ruby wants. |
Underscore methods are also treated specially in Ruby. You're allowed to use the same underscore name for multiple arguments:
|
Well, I learned something new today, too. |
@@ -26,6 +26,7 @@ Ruby | |||
calls. | |||
* Prefer `if` over `unless`. | |||
* Use `_` for unused block parameters. | |||
* Prifix unused variables, or method arguments with `_`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No comma.
Instead of "method arguments", how about "parameters"?
And "Prefix" is misspelled.
Prefix unused variables or parameters with underscore (
_
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.
👍 🇺🇸 |
It sounds like we should merge this. Any objections @georgebrock? |
@gylaz No objections: after everything I've learnt in this PR discussion I'm actively in favour of merging it. |
No description provided.