Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a common idiom in languages where the types do not include range checks. An "out of bounds" value is used to indicate one of several conditions. Here, the return value indicates two things: 1) was the character found, and 2) where was it found. The use of -1 for <code>not found</code> and a non-negative index for <code>found</code> succinctly encodes both of these into one value, and the fact that <code>not-found</code> does not need to return an index.</p> <p>In a language with strict range checking, such as Ada or Pascal, the method might be implemented as (pseudo code)</p> <pre><code> bool indexOf(c:char, position:out Positive); </code></pre> <p><code>Positive</code> is a subtype of int, but restricted to non-negative values.</p> <p>This separates the found/not-found flag from the position. The position is provided as an out parameter - essentialy another return value. It could also be an in-out parameter, to start the search from a given position. Use of -1 to indicate not-found would not be allowed here since it violates range checks on the Positive type.</p> <p>The alternatives in java are:</p> <ul> <li>throw an exception: this is not a good choice here, since not finding a character is not an exceptional condition.</li> <li>split the result into several methods, e.g. <code>boolean indexOf(char c); int lastFoundIndex();</code>. This implies the object must hold on to state, which will not work in a concurrent program, unless the state is stored in thread-local storage, or synchronization is used - all considerable overheads.</li> <li>return the position and found flag separately: such as <code>boolean indexOf(char c, Position pos)</code>. Here, creating the position object may be seen as unnecessary overhead. </li> <li>create a multi-value return type</li> </ul> <p>such as</p> <pre><code>class FindIndex { boolean found; int position; } FindIndex indexOf(char c); </code></pre> <p>although it clearly separates the return values, it suffers object creation overhead. Some of that could be mitigated by passing the <code>FindIndex</code> as a parameter, e.g. </p> <pre><code>FindIndex indexOf(char c, FindIndex start); </code></pre> <p>Incidentally, multiple return values were going to be part of java (oak), but were axed prior to 1.0 to cut time to release. James Gosling <a href="http://www.gotw.ca/publications/c_family_interview.htm" rel="noreferrer">says</a> he wishes they had been included. It's still a <a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4222792" rel="noreferrer">wished-for feature</a>.</p> <p>My take is that use of magic values are a practical way of encoding a multi-valued results (a flag and a value) in a single return value, without requiring excessive object creation overhead.</p> <p>However, if using magic values, it's much nicer to work with if they are consistent across related api calls. For example, </p> <pre><code> // get everything after the first c int index = str.indexOf('c'); String afterC = str.substring(index); </code></pre> <p>Java falls short here, since the use of -1 in the call to <code>substring</code> will cause an <code>IndeOutOfBoundsException</code>. Instead, it might have been more consistent for substring to return "" when invoked with -1, if negative values are considered to start at the end of the string. Critics of magic values for error conditions say that the return value can be ignored (or assumed to be positive). A consistent api that handles these magic values in a useful way would reduce the need to check for -1 and allow for cleaner code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload