44

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?

closed as primarily opinion-based by gnat, superM, Kilian Foth, user40980, World Engineer Mar 13 '14 at 12:35

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

47

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.

  • 34
    I think I would choose canBeEaten or isEdible over eatAble. – kzh Mar 17 '16 at 19:10
  • 2
    @kzh -- isEdible is good! Not so sure about canBeEaten as its something that happens to the object rather than something the object can do. Picky Picky I know :-). – James Anderson Aug 8 '17 at 4:25
  • 1
    What about a flag to indicate whether something is allowed or not? Is "AllowsExtensions" clear? Or would "DoesAllowExtensions" be clearer, even though longer? – Reversed Engineer Feb 8 '18 at 6:42
  • 1
    if you do use -able as a suffix i wouldnt capitalize it as its not a word. e.g. wearable rather than wearAble – jk. Aug 10 '18 at 7:20
  • 2
    @kzh: I initially read eatAble as "is able to eat" rather than "is able to be eaten". All in all, it's ambiguous and would indeed be better avoided. – Flater Aug 10 '18 at 8:05
7

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.

  • 2
    "if the cod is too hard to understand" sticking with the fish/cat theme of the other answer I see – scottyseus Aug 8 '18 at 15:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.