views:

58

answers:

4

I've seen the following in legacy code:

public void someFunction(List myList){
List myList2 = myList;
}

Is there a good reason to re-assign parameters as local variables in a function?

+2  A: 

Not really. Aliasing or reuse of names should be avoided, in my opinion.

Kim Gräsman
A: 

It depends on the language. In some cases, you may want to make changes to the "copy" later on - and in some languages/situations changing the original parameter will make changes to what the caller sees.

If you could say which language you're talking about (Java?) and give a concrete example, that would help us to explain.

Jon Skeet
A: 

There's no good reason to do this, with pass-by-value parameters. This looks like a not-quite-fluent developer, recalling that another language he's worked with bit him if he didn't work on copies of his parameters.

Michael Petrotta
+1  A: 

Could be a personal style. Or a failed attempt to create a new reference? I am pretty sure as-is the compiler discards myList2 in favor of myList.

Rob Elliott
yes, probably compiler will optimize it away.
artificialidiot