67

Should I always use is as prefix for boolean variables? What about booleans that indicate something in past? Should I write isInitialized or wasInitialized? Should I write for properties IsManyMembers or HasManyMembers?

Is there any best practices? Or I should just write in accordance with English rules?

gnat
  • 20,543
  • 29
  • 115
  • 306

2 Answers2

68

Not really, as booleans are not always used to indicate that an object "is" something.

"has" is an equally valid prefix "was", "can" are also valid in particular circumstances, also, I have seen the suffix "Able" used.

So Object herring:-
 isFish = true
 isCat = false
 hasScales = true
 hasFur = false
 canSwim = true
 wasEgg = true
 eatAble = true

Object moggy:-
 isFish = false
 isCat = true
 hasScales = false
 hasFur = true
 canSwim = false
 wasEgg = false
 eatAble = false

It all depends on what makes the program readable.

8

I'd go with English rules. I tend to think about the next coder that is going to look at your work being an axe wielding maniac that is going to come after me if the code is hard to understand. When I keep this in mind the best option for my health is to keep the code clean and easy to read, which means the best English and domain language possible.

Klee
  • 1,313