Naming Conventions for Boolean Attributes

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 just email_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 with email_hidden
  • Ruby on Rails defines a "predicate" version (one that ends in a ?) of your boolean attribute and returns true/false as expected. So, while your attribute is named email_hidden in the database you should reference it as email_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:

  1. Ruby enforces certain naming conventions. Symbols in Ruby can't have question marks. Thus invocations of :my_boolean_attribute? both will fail with a NameError. Edit: not correct, just use the quoted syntax for a symbol, e.g., :"my_attribute?"
  2. 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



Leave a reply



Submit