81

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.

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Berek Bryan
  • 13,755
  • 10
  • 32
  • 43

9 Answers9

81
isBeforeTheLastItem

isInFrontOfTheLastItem

isTowardsTheFrontOfTheList

Maybe too wordy but they may help give you ideas.

shashwat
  • 7,851
  • 9
  • 57
  • 90
Bela
  • 3,437
  • 1
  • 20
  • 12
  • 5
    i think i am sold on 'isBeforeLast'...duh – Berek Bryan Aug 04 '09 at 15:05
  • 16
    Can't `isBeforeLast` be replaced with `!isLast`? – Joe D Aug 23 '10 at 20:42
  • 37
    If there can be elements _after_ "Last", then why is it called "Last"? – Joe D Jan 06 '11 at 18:33
  • 1
    Correct me if I'm wrong, but shouldn't one avoid using articles in naming variables? – Gurgadurgen Jun 20 '14 at 13:36
  • what would be suitable for display=true meaning show some value and display = false meaning don't show a value.isDisplay sounds odd to me! – Thunder Mar 09 '15 at 13:10
  • 5
    @Thunder – `shouldDisplay` – nickiaconis Jun 16 '15 at 20:14
  • probably not a good idea but just to get funky you could use isPenultimate, since [penultimate](http://www.merriam-webster.com/dictionary/penultimate) means 2nd to last. – jxramos Jun 20 '15 at 00:52
  • 3
    How is it in plural case? Should it start with "are"? For example `areAnimalsAllowed`? And in case of possesion? Should it start with "has/have"? For example `hasAnimals`? – dpelisek Aug 05 '15 at 07:02
  • 1
    @Thunder such ambiguous examples just need to be made more verbose. `display = false` or `isDisplay=false` don't **show intent**. But `isDisplayed=false` or `shouldDisplay=false` or `isMeantForDisplay=false` are clearer. @dpelisek i think you're right, that the important part is the presence of some **state of being verb**. it doesn't have to be "is" necessarily. – maurice Aug 22 '16 at 23:02
  • I think prefix "is" the best marker for boolan value. Whenever the name is plural I use "is" because single marker 4 me is more important than proper grammar. Sometimes even when tail words aren't coherenting well, they can explain the essence better. isFilterApply is clearer for me than applyFilter – Igor Yudnikov Jan 17 '18 at 09:01
  • 2
    I've used a mixture of prefixes to address the issue that @maurice presents. They range in short words to distinguish booleans (or functions that return booleans) from other variables. These prefixes come in the form as `is`, `can`, `has`, `was`; but certainly are not limited to them. Example(s): `isVisible`, `hasRecords`, `wasDrawnToScreen`, `canAccessPage`. Enforcement is ultimately a struggle as these rules are writer's-choice, since the goal is for clarity and readability, which is often subjective. – vol7ron Sep 02 '19 at 01:16
36

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.

Jeff Siver
  • 7,434
  • 30
  • 32
  • 1
    to make it more visual: `if (isLast) {…}` …or if it has to be inverted _(my preffered way to keep positive name)_ `if (!isLast) {…}` – Breaker222 Jun 21 '18 at 11:37
17

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

marklam
  • 5,346
  • 1
  • 24
  • 26
15

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.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
4

isPenultimateOrPrior

isBeforeEnd

Greg
  • 16,540
  • 9
  • 51
  • 97
2

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

if (item.last)
    ...

do {
   ...
} until (item.last);
Wil Moore III
  • 6,968
  • 3
  • 36
  • 49
2

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

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • A sibling is something that has a parent in common with another sibling. There are no parents in a flat list. – John Topley Aug 04 '09 at 15:23
  • @John Topley: Yeah, I suppose you're right but you could argue that all items of the list are siblings and that their parent is the list itself.. I guess the isFollowedBySiblings could be renamed to something like isFollowedByItems, or byOtherItems or byMoreItems or something else. I still feel the isNotLast is the better name, unless he could change the meaning of the flag to the opposite and then call it: isLast (wow.. that was long :) – Mike Dinescu Aug 04 '09 at 15:27
  • seems like stackoverflow discourages editing posts for typos since the edit needs to be 6 characters or more? – ahong Dec 19 '18 at 05:36
2

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?

Meredith L. Patterson
  • 4,853
  • 29
  • 30
1

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.

Itay Maman
  • 30,277
  • 10
  • 88
  • 118