[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Use of ? in variable names

Michael Brooks

2/16/2008 6:05:00 PM

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
7 Answers

Phlip

2/16/2008 10:45:00 PM

0

Michael Brooks wrote:

> # This naming convention doesn't works with "?" boolean_variable? = true
> puts boolean_variable?

> So why isn't this allowed? Did I miss something?

Ruby is "Opinionated". The ? is not allowed because "the parser doesn't
work like that".

A variable might become obsolete, so requiring ? to only decorate methods
is a hint those methods should "work for their living", by going out and
actually determining whether 'boolean_variable' is still true - hopefully
without side-effects!

--
Phlip
http://assert2.ruby...

Morton Goldberg

2/16/2008 11:50:00 PM

0

On Feb 16, 2008, at 1:04 PM, Michael Brooks 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

Jari Williamsson

2/17/2008 9:47:00 AM

0

Morton Goldberg wrote:
> On Feb 16, 2008, at 1:04 PM, Michael Brooks 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.

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

Todd Benson

2/17/2008 10:16:00 AM

0

On Feb 16, 2008 12:04 PM, Michael Brooks <michael.brooks@shaw.ca> 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. 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

Todd Benson

2/17/2008 10:21:00 AM

0

On Feb 17, 2008 4:16 AM, Todd Benson <caduceass@gmail.com> wrote:
>
> On Feb 16, 2008 12:04 PM, Michael Brooks <michael.brooks@shaw.ca> 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. 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.

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

Dick Davies

2/17/2008 10:32:00 AM

0

On Feb 16, 2008 6:04 PM, Michael Brooks <michael.brooks@shaw.ca> 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.

--
Rasputnik :: Jack of All Trades - Master of Nuns
http://number9.helloope...

Michael Brooks

2/17/2008 10:27:00 PM

0

Michael Brooks 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