[ACCEPTED]-Why can't a variable name end with `?` while a method name can?-ruby
You'd have to ask Matz to get an authoritative 1 answer. However,
- Ruby is an untyped programming language and a variable like
finished?
would imply a specific type (boolean) which appears somewhat contradictory to me. - A question somewhat requires a receiver (who can answer the question). A method must have a receiver (the object the method is called on), so a question mark makes sense. A variable on the other hand has no receiver, it's a mere container.
Now this is just a thought, but I think 16 methods with names like empty?
suggest that a 15 some sort of check has to be made inside 14 and object or a class (depending on the 13 context). This check or evaluation means 12 an action must be done. Overall, since we 11 are asking (thus, ?
) object for some state, means 10 there is a possibility that object's state 9 could change throughout the application's 8 lifecycle. A variable could be outdated, but 7 ?
-method (check) will be done in the specific 6 moment, thus providing an up-to-date information 5 on some state that could be presented in 4 a boolean form.
So I'd like to think that 3 this is a design constraint provided by 2 the architect (Matz) to enforce a more logical, close-to-real-life 1 coding approach.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.