Naming conventions for boolean attributes
I think of it like this:
- A boolean attribute should answer a simple yes/no question.
- Asking "is?" is a simple way to ask a yes/no question.
- So a boolean attribute name should complete the sentence
is <attribute_name>
- Then just remove the
is
because Ruby prefers the?
suffix for "is"-style method names (see below for more), leaving you with justemail_hidden
So, for your specific case, name your attribute like this:
- Start with the question "email is hidden"?. Which can be represented in code as:
email_is_hidden
. - Remove the
is
and you're left withemail_hidden
- Ruby on Rails defines a "predicate" version (one that ends in a
?
) of your boolean attribute and returnstrue
/false
as expected. So, while your attribute is namedemail_hidden
in the database you should reference it asemail_hidden?
in your code for clarity and as is the Ruby on Rails convention.
Naming convention for Boolean properties
Use both. It makes your code much more readable:
if (IsPrintable) Print()
if (HasDocument) Documents[0].Name = 'New Doc'
Etc.
Ruby: Boolean attribute naming convention and use
Edit: three-years later; the times, they are a-changin'…
Julik's answer is the simplest and best way to tackle the problem these days:
class Foo
attr_accessor :dead
alias_method :dead?, :dead # will pick up the reader method
end
My answer to the original question follows, for posterity…
The short version:
You can't use a question mark in the name of an instance variable.
The longer version:
Take, for example, attr_accessor :foo
— it's simply conceptually a bit of syntactic sugar for the following:
def foo
@foo
end
def foo=(newfoo)
@foo = newfoo
end
Furthermore, the question-mark suffix is mostly just a convention to indicate that the return value of a method is a boolean.
The best approximation I can make of what you're going for here…
class MyClass
def initialize
@awesome = true
end
def awesome?
@awesome
end
end
In this case, there may be a case to be made for using attr_accessor
— after all, it may be explicit that you're working directly with a boolean attribute. Generally, I save the question-mark suffix for when I am implementing a method whose boolean return value is based on slightly more complex conditions than just the value of an attribute.
Cheers!
Edit, two years later, after a recent comment:
- Ruby enforces certain naming conventions.
Symbols in Ruby can't have question marks. Thus invocations ofEdit: not correct, just use the quoted syntax for a symbol, e.g.,:my_boolean_attribute?
both will fail with aNameError
.:"my_attribute?"
- Symbols are immutable, attempting to assign to one will throw a
SyntaxError
.
Python boolean methods naming convention
There is no standard naming convention specific to boolean-returning methods. However, PEP8 does have a guide for naming functions.
Function names should be lowercase, with words separated by
underscores as necessary to improve readability.
Typically, people start a function with is
(e.g. is_palindrome
) to describe that a boolean value is returned.
Java boolean getters is vs are
I can't remember which book this was from, but the essence is that code will be read many more times than it's written. Write for readability.
For a boolean field, what is the naming convention for its getter/setter?
Suppose you have
boolean active;
Accessors method would be
public boolean isActive(){return this.active;}
public void setActive(boolean active){this.active = active;}
See Also
- Java Programming/Java Beans
- Code Conventions for the Java Programming Language
Boolean naming convention: statement or questionn?
A Boolean variable or property is an outcome of an expression, therefore, it is not a question, but a statement.
If, instead, you are asking a question, which requires an operation to answer it, than it should be a method and may be named as such.
Related Topics
How to Use Coffeescript in the Views Executed on Render.Js
How to Calculate Next, Previous Business Day in Rails
How to Use Present? in Ruby Projects
What's the Best Way to Start a Background Process, That Can Get Accessed Later On
Ruby on Rails - Rack-Cors Multiple Origins with Different Resources
Colon in the Front: Yaml Syntax
Why Is Foreman Gem Ignoring the Port Environment Variable
Which Ruby Rest API Client for Neo4J
Dynamically Get Object's Attribute
Using Rest-Client to Download a File to Disk Without Loading It All in Memory First
Form_For with Multiple Controller Actions for Submit
Unwanted Form Parameters Being Appended to Pagination Links
Can You Create/Write/Append a String to a File in a Single Line in Ruby
Ruby: How to Join Elements of an Array Together with a Prefix
What's The Most Efficient Way Get The First Day of The Current Month