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?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityUse 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.
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.
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?).
Key.IsValid
andPost.Thumbnails.Any
:)