• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variable Naming Conventions - 4 Say

 
Brad Ford
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick question on naming conventions:
Let's say I have a boolean variable that is the result of figuring out if a number is divisible by 4 and not by 100, what would be a good name? I'm thinking isDivBy4Not100.
Any rules of thumb on this? I know that booleans should be prefaced with is but beyond that, is it just for readability/meaning?
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brad Ford:
I'm thinking isDivBy4Not100.

It's better to keep the negatives out of the identifiers.

I know that booleans should be prefaced with is but beyond that, is it just for readability/meaning?

Yep.
 
Brad Ford
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. I put the wrong exercise in the topic title - should be 3 Leap Year.
Ok. So no negatives in the var name - any suggestion on what to name that beast?
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about two variables:
isDivBy4
isDivBy100
Bill
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its going to pretty difficult get the NOT out of the variable name if you insist putting the result of these two tests in one boolean variable.
Another approach might to use say something like isDivBy4AndBy400 or use two boolean variables
Good luck
 
Brad Ford
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, in terms of the app itself, I don't need any of the variables, the if statement could bypass these by doing the boolean tests directly. Thing is, in the style guide, it's recommended that that be avoided by making vars out of the tests so that the if statement is shorter. Just thought that ending up with three vars (there's another to test for as well) is almost as bad as directly testing. I'll go with three var names and prepare to be nitpicked...
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the rule of thumb is readability. Putting all three tests into one if statement without identifiers would be pretty hard to read. But if you set all three identifiers up first and then just check those in an if statement, anyone reading it could immediately see what you are doing.
if( isFirstIdentifer && isSecondIdentifier )
is much easier to read than
if( 100 / 4 == 20 && 100 * 4 == 20 )
(I obviously changed all the values and if statement so as not to give anything away, but to still make my point.)
Bill
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Style Guide it says "Methods that retrieve a
boolean object state must use the prefix is in the
method name. example: isEnabled()."
Throughout the Style Guide only methods returning a
boolean are prefixed with is and all boolean vars, except
for the example in section 2.2 - Spacing, are not prefixed
with is.
boolean done = false ;
boolean isLeapYear() { boolean y4 = (... ); }
Soooo, are we supposed to prefix all booleans with
is or just methods returning boolean. And isn't this
getting into Hungarian notation? I personally like thang -
thangALeapYear, thangDone
Brad, sorry I don't have any answers only more questions.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going back to the notion that all method should be verbs (because they do something) and all variables should be nouns (because they are something) I would agree with the style guide. You call a method that returns a boolean so you are saying isThisTrue, you are asking it to give you an answer.
For a variable, you could do the same thing, but you don't have to. I tend not to because it helps me keep my methods and variables clear again. So I would say boolean divBy100 and not isDivBy100, but as the style guide doesn't bring it up, it is optional.
As for Hungarian notation, I don't think it falls into that category. It is really a method telling you what it does, isThisTrue(). Hungarian notation would be bolThisTrue which isn't as readable.
Just my opinion on the matter.
Bill
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used to have the impression that identifiers beginning with 'is' were pretty much limited to methods returning a boolean. However I found out that it's ok to use stuff like "is" for variables too. It's not required and it's not taboo.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic