java - Is it problematic to assign a new value to a method parameter? -


eclipse has option warn on assignment method's parameter (inside method), in:

public void dofoo(int a){    if (a<0){       a=0; // generate warning    }    // stuff } 

normally try activate (and heed) available compiler warnings, in case i'm not sure whether it's worth it.

i see legitimate cases changing parameter in method (e.g.: allowing parameter "unset" (e.g. null) , automatically substituting default value), few situations cause problems, except might bit confusing reassign parameter in middle of method.

do use such warnings? why / why not?

note:

avoiding warning of course equivalent making method parameter final (only it's compiler error :-)). question why should use keyword "final" on method parameter in java? might related.

for me, long , clearly, it's fine. say, doing buried deep in 4 conditionals half-way 30-line function less ideal.

you have careful when doing object references, since calling methods on object given may change state , communicate information caller, of course if you've subbed in own placeholder, information not communicated.

the flip side declaring new variable , assigning argument (or default if argument needs defaulting) may clearer, , not less efficient -- decent compiler (whether primary compiler or jit) optimize out when feasible.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -