Ahmet Eroğlu
Ahmet Eroğlu

Reputation: 724

If condition naming convention

I have a boolean function that checks if scandata keys are valid. I named it as :

areScandataKeysValid()

For naming conventions, should it be named as isScandataDataKeysValid() or like how I did above?. In Clean Code book, I only saw examples with is version. So, I'm confused which one makes sense? Grammatically, "are" thats for sure.

Upvotes: 0

Views: 1066

Answers (2)

August Karlstrom
August Karlstrom

Reputation: 11377

I think "is" or "are" is superfluous. The most important thing is that a predicate function has an adjective as the last word. Therefor I would name the method scanDataKeysValid, or if it's clear from the context of the class, simply keysValid.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718886

The isName() convention is part of the JavaBeans standard, and applies to boolean-valued properties.

  • If your API does not need to be JavaBeans compatible, you can ignore the convention.

  • If your method for checking the scan keys is not intended as a getter for a "property", the isName() convention does not apply.

I share your preference for areScandataKeysValid over isScandataKeysValid, because of the grammatical dissonance. There are other alternatives to consider; e.g.

  • hasValidScandataKeys for a predicate, or
  • validateScandataKeys or checkScandataKeys for a method that (typically) throws an exception to indicate invalidity.

I am not aware of any documented conventions (i.e. style guides) that favor any one of the above. (Nothing says that "good grammar" is essential.)

Bottom line: your choice.

Upvotes: 2

Related Questions