The parameter 'foo' should not be assigned -- what's the harm?

Viewed 19228

Compare this method:

void doStuff(String val) {
    if (val == null) {
        val = DEFAULT_VALUE;
    }

    // lots of complex processing on val
}

... to this method:

void doStuff(String origVal) {
    String val = origVal;
    if (val == null) {
        val = DEFAULT_VALUE;
    }

    // lots of complex processing on val
}

For the former method, Eclipse emits the warning "The parameter 'val' should not be assigned". Why?

To my eye, the former is cleaner. For one thing, it doesn't force me to come up with two good names for val (coming up with one good one is hard enough).

(Note: Assume there is no field named val in the enclosing class.)

5 Answers
Related