[ruby-core:93647] [Ruby master Feature#15991] Allow questionmarks in variable names

From: jeremie.bonal@...
Date: 2019-07-09 23:18:51 UTC
List: ruby-core #93647
Issue #15991 has been updated by aquaj (J駻駑ie Bonal).


shevegen (Robert A. Heiler) wrote:
> I do not see why this would make the design more "coherent", per se.

I guess "design coherence" is a very subjective notion \^\^" 
What I meant by that is that in most cases in Ruby, whether an identifier is a local variable or a method call is transparent.
I've also often heard (especially when talking about why methods are a bit clunky to pass around in Ruby) that it was a conscious design choice that did matter to matz.
Because of that it feels "natural" (coherent? ;-)) to me that the naming rules should be more uniform between local variables and methods.

shevegen (Robert A. Heiler) wrote:
> I do not see why adding "?" to variable names as such would require of one to also add "!"
> as well. I often don't fully understand why this is said, either. "?" and "!" do not have
> the same, and not even opposing use cases either. Their use cases and rationales are
> different.

I do think this discussion is a bit out of the scope of the issue, but I wanted to bring it up because I was writing the proposal with this idea of making naming rules more uniform. 
If local variable names end up allowing trailing `?`s, I think trailing `!`s would be the last remaining difference between local variable names and method names. I figured it might be brought up while discussing the issue so I went ahead and mentioned it but I realize now it might just have made the proposal a bit more confusing.

shevegen (Robert A. Heiler) wrote:
> I think this is a backwards-incompatible change so introduction would require more thought.
I don't see any behaviour this could break in existing code.
What are for now method lookups would turn into variable-or-method lookups, and since it's currently impossible to assign to a `?`-ended variable name, it would always default to the method lookup.
Am I missing something here ?

shevegen (Robert A. Heiler) wrote:
> One drawback of this proposal, if implemented, will be that people would no longer know
> whether:
> 
>     foo?
> 
> Is a variable; or a method call. Right now they know that it must be a method call. Again,
> we can see pros/cons either way, but we need to mention cons too.

As can be surely gathered from the beginning of this reply: Where you see a drawback, I see a feature here, and it's actually what's motivating this proposal. Once again, everything is a matter of opinion I guess :-)

--

On the matter of allowing it in instance variable names too, I'm personally against it. An instance variable is clearly different from a method call at first glance. I understand the appeal given the code examples that were shown, but this feels like a mean to an end (`attr_reader :foo?`) and I'm not sure it'd be the best way forward.


----------------------------------------
Feature #15991: Allow questionmarks in variable names
https://bugs.ruby-lang.org/issues/15991#change-79255

* Author: aquaj (J駻駑ie Bonal)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hi,

I thought such an issue would've already been discussed but no number of searches allowed me to find a similar request. Feel free to close if I missed a previous refusal.

From time to time, especially when trying to clear up complex conditional logic, I find myself wishing I could add `?` to variable names, since I got used to it while naming methods.

For example, currently:
```
if (node? && terminal?) || (halting && (value == halting))
  # ...
end
```
becomes
```
last_node = self.node? && self.terminal?
halt_on_node = halting && (value == halting)
if last_node || halt_on_node
  # ...
end
```

`halt_on_node` is clear enough, but `last_node` feels like it would contain a node, instead of expressing its actual purpose ("is the node the last one?").
Right now a developer would have two options as I see them:
1 - extract the conditional to a method `def last_node?` which can be a bit much if it's the only place this code is called.
2 - rename the variable something like `is_last_node`, which feels a bit silly since we're in ruby and used to seeing `?`s for predicates. 

Trying to assign to a questionmarked variable (`a? = true`) raises a `SyntaxError`. IMHO, it would make for more coherent design to allow it, just like we do in method names.

I was afraid that `variable?` would be already parsed as beginning a ternary expression (`variable?1:3`) but this isn't parsed either, the only thing it's used for is for method calls (`a?5 <==> a?(5)`), so this change wouldn't disrupt any current behavior, the expression would just be looked up like any other call instead of only looking up methods.

The only thing I can see with this is that it might raise the issue of allowing `!`s in variable names too, which I'm not sure makes a lot of sense (unlike `?` which denotes "booleanness", a trait shared by variables and methods alike, I can't see how a variable would be "dangerous").




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next