Naming Conventions: What to name a boolean variable?

Coding StyleNaming Conventions

Coding Style Problem Overview


I need a good variable name for a boolean value that returns false when an object is the last in a list.

The only decent name I can come up with is 'inFront', but I don't think that is descriptive enough.

Another choose would be 'isNotLast'. This is not good practice though (Code Complete, page 269, Use positive boolean variable names).

I am aware that I could change the variable definition. So true is returned when an object is the last and call the variable 'isLast', however, it would make this task easier if I had the first explanation.

Coding Style Solutions


Solution 1 - Coding Style

isBeforeTheLastItem

isInFrontOfTheLastItem

isTowardsTheFrontOfTheList

Maybe too wordy but they may help give you ideas.

Solution 2 - Coding Style

My vote would be to name it IsLast and change the functionality. If that isn't really an option, I'd leave the name as IsNotLast.

I agree with Code Complete (Use positive boolean variable names), I also believe that rules are made to be broken. The key is to break them only when you absoluately have to. In this case, none of the alternative names are as clear as the name that "breaks" the rule. So this is one of those times where breaking the rule can be okay.

Solution 3 - Coding Style

hasFollowingItems? or hasFollowingXXXXs where XXXX is whatever the item in your list is?

Solution 4 - Coding Style

Personally more than anything I would change the logic, or look at the business rules to see if they dictate any potential naming.

Since, the actual condition that toggles the boolean is actually the act of being "last". I would say that switching the logic, and naming it "IsLastItem" or similar would be a more preferred method.

Solution 5 - Coding Style

isPenultimateOrPrior

isBeforeEnd

Solution 6 - Coding Style

How about:

 hasSiblings
 or isFollowedBySiblings (or isFolloedByItems, or isFollowedByOtherItems etc.)
 or moreItems

Although I think that even though you shouldn't make a habit of braking 'the rules' sometimes the best way to accomplish something may be to make an exception of the rule (Code Complete guidelines), and in your case, name the variable isNotLast

Solution 7 - Coding Style

Haskell uses init to refer to all but the last element of a list (the inverse of tail, basically); would isInInit work, or is that too opaque?

Solution 8 - Coding Style

A simple semantic name would be last. This would allow code always positive code like:

if (item.last)
    ...

do {
   ...
} until (item.last);

Solution 9 - Coding Style

Two issues to think about

  1. What is the scope of the variable (in other words: are you speaking about a local variable or a field?) ? A local variable has a narrower scope compared to a field. In particular, if the variable is used inside a relatively short method I would not care so much about its name. When the scope is large naming is more important.

  2. I think there's an inherent conflict in the way you treat this variable. On the one hand you say "false when an object is the last in a list", where on the other hand you also want to call it "inFront". An object that is (not) last in the list does not strike me as (not) inFront. This I would go with isLast.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBerek BryanView Question on Stackoverflow
Solution 1 - Coding StyleBelaView Answer on Stackoverflow
Solution 2 - Coding StyleJeff SiverView Answer on Stackoverflow
Solution 3 - Coding StylemarklamView Answer on Stackoverflow
Solution 4 - Coding StyleMitchel SellersView Answer on Stackoverflow
Solution 5 - Coding StyleGregView Answer on Stackoverflow
Solution 6 - Coding StyleMike DinescuView Answer on Stackoverflow
Solution 7 - Coding StyleMeredith L. PattersonView Answer on Stackoverflow
Solution 8 - Coding StyleWil Moore IIIView Answer on Stackoverflow
Solution 9 - Coding StyleItay MamanView Answer on Stackoverflow