Okay, they’re not always evil. Counters and other pseudo-locals are fine. Problems arise when mutable local variables are used as return values for functions/methods. Reassigning a local variable, even once, can have a dramatic negative impact on readability.

Anyone who has ever paired with me knows this type of method/function is anathema. Assume this is an instance method on UserStory.

public UserStory getTopMostParent() {
  UserStory topMostParent = null;
  if(getParent() == null) {
    topMostParent = this;
  } else {
    topMostParent = getParent().getTopMostParent();
  }
  return topMostParent;
}

Often you’ll see methods written like this because someone’s first mentor or most influential university professor advocated the structured programming practice of “one return statement per function”.

Whatever the reason, it’s bullshit.

I’ve heard various arguments in favor of “one return statement per function” but none that were good. One oft-cited explanation is the mathematical provability of one return per blah blah.

I’ve said before that readability is the most important -ility, and I stick by it. Any other -ility (except testability) is bullshit (until it’s not).

The above function should always (ALWAYS) be written like this:

public UserStory getTopMostParent() {
  if(getParent() == null) {
    return this;
  }
  return getParent().getTopMostParent();
}

This is a dramatic improvement in readability. Do it. There is absolutely no reason not to. One could argue that the local return variable might be reassigned depending on other conditional expressions in the function. I’d tell you to compose your methods.

One last thing – that last method could potentially have an “else” around the return getParent().getTopMostParent(). Don’t do it. It’s the last statement in the method. The else is redundant. Putting it in an else block adds one more thing you need to read to understand the method.