Modifying arguments 'passed by value' inside a function and using them as local variables

I have seen some code in which the arguments passed to the function by value was being modified or assigned a new value and was being used like a local variable.

Is it a good thing to do? Are there any pitfalls of doing this or is it Ok to code like this?

Answers


Essentially, a parameter of a function is a local variable, so this practice is not bad in principle.

On the other hand, doing this can lead to maintenance headaches. If another programmer comes along later, he might expect the variable to hold the passed in value, and the change will cause a bug.

One justification for reusing the variable is for a misguided notion of efficiency of memory usage. Actually, it can't improve efficiency, and can decrease it. The reason is that the compiler can automatically detect if it is useful to use the same register for two different variables at two different times, and will do it if it is better. But the programmer should not make that decision for the compiler. That will limit the choices the compiler can make.

The safest practice is to use a new variable if it needs a new value, and rely on the compiler to make it efficient.


Need Your Help

Text search using python in mongo db

python mongodb full-text-search pymongo

I created indices on the text fields, the following query runs fine in Mongo.

HOW to understand custom drawn ? Which tools can I use?

c++ c windows user-interface custom-draw

I want to know the tools that beautiful UI programs using?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.