65

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
  • 21,442
  • 29
  • 112
  • 288
Mark Twain
  • 761
  • 1
  • 5
  • 6
  • 3
    recommended reading: **[Why is asking a question on “best practice” a bad thing?](http://meta.stackexchange.com/a/142354/165773)** – gnat Mar 13 '14 at 06:15
  • 2
    Related (and closed) http://programmers.stackexchange.com/questions/221868/should-methods-that-return-boolean-be-named-after-a-question-or-an-assertion – superM Mar 13 '14 at 07:38

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.

James Anderson
  • 18,049
  • 1
  • 42
  • 72
  • 46
    I think I would choose canBeEaten or isEdible over eatAble. – kzh Mar 17 '16 at 19:10
  • 5
    @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 08 '17 at 04: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 08 '18 at 06: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 07: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 08:05
  • 1
    @DaveBoltman: I don't see an issue with `AllowExtensions`. I think the gist of boolean naming is that it's a terse binary question. "does allow" (as opposed to "allows") is more grammatically correct (as a question) but it is equally understandable in context of terse naming so I would pick the simpler "allows" (note that I would prefer `AllowExtensions` over `AllowsExtensions` - but I'm not sure if that's too subjective) – Flater Aug 10 '18 at 08:08
  • 2
    @Flater sure I agree with you. Terse is better for readability. I saw somewhere that including a verb is useful for booleans. "Is", "Are", "Allow", "Has" are all (conjugated) verbs, which makes sense to me, whereas just "Extensions"is ambiguous in meaning. – Reversed Engineer Aug 16 '18 at 10:26
  • 2
    Just to add another one: I found that I often use the prefix `should`, e. g. `shouldBePrinted` (of course you could also substitute it with `mustBePrinted` or `hasToBePrinted`) – Thomas Leu Nov 14 '18 at 14:50
  • @ReversedEngineer another alternative is to find an equivalent adjective if possible/exists, e.g. `AllowsExtension` -> `IsExtensible`, though the meaning might be lost and need more explanation if `Extension` is a specific term in the app's context. – Andrew T. Aug 09 '19 at 07:42
  • @JamesAnderson : How would you name a variable which determines whether to export additional information to a file? do_export, should_export, flag_export, export, hastobe_exported, ... ? – gebbissimo Apr 28 '20 at 10:09
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
  • 7
  • 9