Use of ? in variable names

Hello everyone:

Why can’t I name boolean variables with a “?” on the end? The “?” can
be used on the end of methods but not variable. Here is an example:

This naming convention works with “?”

def boolean_method?
return true
end
puts boolean_method?

This naming convention doesn’t works with “?”

boolean_variable? = true
puts boolean_variable?

The question mark would make the intention of the variable obvious, just
like it helps the method name.

So why isn’t this allowed? Did I miss something?

Thank You,

Michael

On Feb 16, 2008, at 1:04 PM, Michael B. wrote:

Hello everyone:

Why can’t I name boolean variables with a “?” on the end? The “?”
can be used on the end of methods but not variable.

Because the rules for naming methods are different from the rules for
naming variables.

So why isn’t this allowed? Did I miss something?

You aren’t missing anything – you’re just in denial over Ruby’s
lexical rules. Maybe you can convince Matz to change the rules to way
you want them to work. But I doubt it.

Regards, Morton

On Feb 16, 2008 12:04 PM, Michael B. [email protected] wrote:

This naming convention doesn’t works with “?”

boolean_variable? = true
puts boolean_variable?

The question mark would make the intention of the variable obvious, just
like it helps the method name.

So why isn’t this allowed? Did I miss something?

I suppose because any object is true and the following wouldn’t make
sense…

boolean_variable? = “false”

You want to ‘type’ a variable (the syntax defines its purpose), which
goes against the general grain of Ruby.

Todd

Morton G. wrote:

So why isn’t this allowed? Did I miss something?

You aren’t missing anything – you’re just in denial over Ruby’s lexical
rules. Maybe you can convince Matz to change the rules to way you want
them to work. But I doubt it.

What would it add, in a world of short methods and encapsulation of
instance variables? I also feel there’s a difference between a ? query
method that returns a boolean result and a static boolean value.

Best regards,

Jari W.

On Feb 16, 2008 6:04 PM, Michael B. [email protected] wrote:

Hello everyone:

Why can’t I name boolean variables with a “?” on the end? The “?” can
be used on the end of methods but not variable.
The question mark would make the intention of the variable obvious, just
like it helps the method name.

To me, the method is asking a question (everything.ok?) which gets a
true/false
answer.
Whereas the variable assignment is a statement (ok = true) not a
question,
hence the lack of a question mark.

On Feb 17, 2008 4:16 AM, Todd B. [email protected] wrote:

end

I suppose because any object is true and the following wouldn’t make sense…

boolean_variable? = “false”

You want to ‘type’ a variable (the syntax defines its purpose), which
goes against the general grain of Ruby.

I guess that didn’t really come out right. What you suggest is to
require variables of certain spelling to have a defined type
(boolean). Convention is fine for methods, but for variables, I think
it should be loose.

Todd

Michael B. wrote:

Hello everyone:

Why can’t I name boolean variables with a “?” on the end? The “?” can
be used on the end of methods but not variable.

Thank you everyone for your replies.

I realize that booleans aren’t really a type in Ruby and I don’t have a
problem with that. It’s actually very cool that Ruby is designed to
work that way. Especially considering that variable being used as a
boolean in Ruby can also me set to nil to indicate “answer unknown” like
booleans in databases.

I’m not intending the “?” to control the parser or anything. It’s just
“decoration” to remind me that the variable or method is intended to be
treated like a boolean when I’m looking at my code. The “?” suffix is
simpler than a “is_”, “has_” prefix or “_flag” suffix for labeling
booleans. I believe one or two Ruby book even recommend that methods
which act like booleans should end with names that have a “?”.

So, its unfortunate that I can’t put a “?” on the end of variables that
do the same thing. I realize I can wrap a variable in a method but
that’s a lot more code just to have boolean variables named the way I
want. And coding that way gets even uglier when I’m only using the
variable privately within a method in which case putting wrappers around
it would look weird.

Oh well… I guess the answer is just “you can’t do it” and I’ll have to
leave it at that.

Thanks again for your replies.

Michael

Michael B. wrote:

I’m not intending the “?” to control the parser or anything. It’s just
“decoration” to remind me that the variable or method is intended to be
treated like a boolean when I’m looking at my code. The “?” suffix is
simpler than a “is_”, “has_” prefix or “_flag” suffix for labeling
booleans. I believe one or two Ruby book even recommend that methods
which act like booleans should end with names that have a “?”.

I would also support this, and I have another case. I often use the
idiom exemplified by:

EXTERNAL_COMMAND = lambda {|data| “command_name -abc #{data} -def”}
or
INCLUDE_RECORD = lambda {|record| (record[0] == “this”) && (/.txt$/ ===
record[1])}

Where I could make the two constants full-blown methods, but I really
like the way ruby closures allow me to separate what are essentially
methods into a form that is both lightweight and suggestive that they
are not first-class citizens of my business logic.

In the latter example I would really like to be able to write
INCLUDE_RECORD? = … In this case the naming convention would be
exactly like that that is permitted and recommended for methods. I see
no strong reason why this couldn’t be allowed for all variable names. I
assume currently the syntax:

bool ? :

Can be written:

bool? :

So I suppose it depends how sacred the latter case is considered.
Personally I wouldn’t mind if a space between the boolean and question
mark was required.

Silas Davis wrote:

I assume currently the syntax:

bool ? :

Can be written:

bool? :

You assume wrongly.

a = 1
=> 1

a? “foo” : “bar”
SyntaxError: compile error
(irb):2: syntax error, unexpected ‘:’, expecting $end
a? “foo” : “bar”
^
from (irb):2
from :0

a ? “foo” : “bar”
=> “foo”

RUBY_DESCRIPTION
=> “ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]”

Todd B. wrote:

I suppose because any object is true

nil and false are objects too.

You can probably argue that flag variables could be called ‘foo?’, but
it’s more difficult to argue for a variable called ‘bar!’, which you’d
need for consistency.

I can think of one minor advantage of not allowing local variables to
end with ? or ! - Ruby unambigously knows that the token is a method,
which improves the error message.

a = 123
=> 123

a?
NoMethodError: undefined method `a?’ for main:Object
from (irb):3
from :0

b
NameError: undefined local variable or method `b’ for main:Object
from (irb):2
from :0