-6

Examples:

isKeyValid -> keyIsValid 
hasPostThumbnail -> postHasThumbnail

The first case is easier to remember (convention). The second is easier/more natural to read.

Which should I use?

5
  • 4
    I wouldn´t go with neither of the options you provided in the example. I would prefer objects Key.IsValid and Post.Thumbnails.Any :)
    – Alex B.
    May 4, 2016 at 16:37
  • The important part is to make it clear what "true" means - which both your examples do. Personaly I go with the is/has first to flag that it is a bool May 4, 2016 at 17:10
  • 1
    It's entirely up to you or your team. Pick a convention and stick to it. There is no absolute correct answer here.
    – MetaFight
    May 4, 2016 at 17:41
  • @AlexB. that would require knowledge of the objects, which may be entirely unimportant. Or difficult, if the class could hold various different kinds of keys, and keyIsValid handles this as long as there is a key that is valid.
    – gnasher729
    May 4, 2016 at 18:41
  • I'll settle for easier to remember over easier to read, but I think the first example is both.
    – JeffO
    May 4, 2016 at 18:52

2 Answers 2

0

Use which ever convention you prefer and live with the consequences. Most readers of your code will know how to interpret "is" and "has" prefixes. For this reason I recommend using the convention. It is helpful to others.

In addition, consider what "makes sense grammatically" actually means in a coding context. Don't confuse the grammar of your spoken language and the naming conventions of the coding language. "IsKeyValid" and "KeyIsValid" or even "Key.IsValid" are all "things" with names, not sentences. When combined with the syntax of the language, they will be interpreted into English sentences depending on the context. For example: if (IsKeyValid) {} will be interpreted as "if the key is valid..." and if (!Key.IsValid) {} will be interpreted as "if the key is not valid...". An absurd outlier example might be if (!_bool_VALIDCONST_glbKeys){}. Yet it will be interpreted the same way, but with greater difficulty and time. Thus naming conventions.

0

It is not necessary to always start with 'is' or 'has'. In some situations, other words may be applicable, but should still have a clear indication of what True / False responses mean. e.g.

  • canMarry - True if over the age of consent and either (is not married or in a culture that allows polygamy)
  • wasMarried - False if never married or (is divorced / widowed and not remarried)

Bear in mind that Boolean states are not common in the real world; gray-scales are more common. For example, 'canMarry' does not cater for legal exclusions such as not being able to marry your own mother; 'wasMarried' requires a definition of what constitutes marriage (does it include legal Civil Partnerships, which are treated as equal to marriage for some pension schemes but not all pension schemes?).

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