Use of ? in variable names

M

Michael Brooks

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
 
M

Morton Goldberg

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
 
J

Jari Williamsson

Morton said:
Because the rules for naming methods are different from the rules for
naming variables.


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 Williamsson
 
T

Todd Benson

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?

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
 
T

Todd Benson

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
 
D

Dick Davies

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.
 
M

Michael Brooks

Michael said:
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
 
S

Silas Davis

Michael said:
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 ? <action if true> : <action if false>

Can be written:

bool? <action if true> : <action if false>

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.
 
B

Brian Candler

Silas said:
I assume currently the syntax:

bool ? <action if true> : <action if false>

Can be written:

bool? <action if true> : <action if false>

You assume wrongly.
SyntaxError: compile error
(irb):2: syntax error, unexpected ':', expecting $end
a? "foo" : "bar"
^
from (irb):2
from :0=> "ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]"
 
B

Brian Candler

Todd said:
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.
NoMethodError: undefined method `a?' for main:Object
from (irb):3
from :0
NameError: undefined local variable or method `b' for main:Object
from (irb):2
from :0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top