View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Forget to return one or more values from methods that require it

If the method is supposed to return a value then don't forget to return the value. If it can return more than one value then be sure to return all values.

For example if you are supposed to write a method that returns the largest item in an array:
public static int findLargest(int[] numArray)
{
  int largest = numArray[0];
  for (int i = 1; i < numArray.length; i++)
  {
    if (numArray[i] > largest)
      largest = numArray[i];
  }
}


forgets to return anything. The method should be:

public static int findLargest(int[] numArray)
{
  int largest = numArray[0];
  for (int i = 1; i < numArray.length; i++)
  {
    if (numArray[i] > largest)
      largest = numArray[i];
  }
  return largest;
}


if the desired item is found return the index it was found at else return -1 to show that it wasn't found. Don't forget to
return both the found index and the -1 if it isn't found.


Link to this Page