This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Variable Naming Conventions - 4 Say Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Variable Naming Conventions - 4 Say" Watch "Variable Naming Conventions - 4 Say" New topic
Author

Variable Naming Conventions - 4 Say

Brad Ford
Ranch Hand

Joined: Mar 22, 2001
Posts: 40
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

Joined: Jul 22, 2000
Posts: 9046
    
  10
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.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Brad Ford
Ranch Hand

Joined: Mar 22, 2001
Posts: 40
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

Joined: Jun 30, 2000
Posts: 1070
How about two variables:
isDivBy4
isDivBy100
Bill
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
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

Joined: Mar 22, 2001
Posts: 40
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Mar 01, 2001
Posts: 233
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Jul 22, 2000
Posts: 9046
    
  10
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.
 
GeeCON Prague 2014
 
subject: Variable Naming Conventions - 4 Say