commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stephen Colebourne" <>
Subject Re: [lang] ArrayUtils.lastIndex
Date Wed, 13 Oct 2004 21:26:25 GMT
My argument is that -1 is *such* a well-known value for not found that
doesn't need to be defined as a constant. Sun doesn't define it as a
constant in String or List for example. Nor does it define the empty string.

It depends on whether you in your calling code want to write:
  if (ArrayUtils.indexOf(array, obj) == -1) {
  if (ArrayUtils.indexOf(array, obj) == ArrayUtils.INDEX_NOT_FOUND) {

I prefer the former, you appear to prefer the latter. There is no right or
wrong answer here, hence my -0, not a -1 to the proposal.

However I will -1 if the javadoc of the method is changed to say
"returns the constant INDEX_NOT_FOUND if the index is not found".
To me that really obfuscates the meaning.


----- Original Message -----
From: "Gary Gregory" <>
Note that one always free to ignore constants and use magic numbers in
their code, no matter what the library does internally.

> Please note that my view pertains only to basic values, like -1 and
> When
> the value refers to something more abstract, like an enumerated
> then a constant makes the code much better and more maintainable. I
> have a place where I draw the line.

A -1 "index" IS abstract!

-1 represents an idea, not an index. This is a classic case where an API
can return an integer value [0..Integer.MAX_VALUE] which represents one
concept: an index; and under other circumstances, the API returns still
an integer, but this time representing a completely different idea:
there is no index.

Isn't this a perfect example of a "magic number"?

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message