Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using function arguments as local variables

Something like this (yes, this doesn't deal with some edge cases - that's not the point):

int CountDigits(int num) {
    int count = 1;
    while (num >= 10) {
        count++;
        num /= 10;
    }
    return count;
}

What's your opinion about this? That is, using function arguments as local variables.
Both are placed on the stack, and pretty much identical performance wise, I'm wondering about the best-practices aspects of this.
I feel like an idiot when I add an additional and quite redundant line to that function consisting of int numCopy = num, however it does bug me.
What do you think? Should this be avoided?

like image 365
Rubys Avatar asked May 02 '10 12:05

Rubys


People also ask

Are function arguments local variables?

The function parameters are also considered local variables. Its scope of validity is identical to the local variables in a function with the exception that they start the execution with an initial value given by the code executing the function call.

Can function arguments be variables?

In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable.

Are function parameters local variables in Python?

An assignment statement in a function creates a local variable for the variable on the left hand side of the assignment operator. It is called local because this variable only exists inside the function and you cannot use it outside.

Can a function be used as an argument?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions.


1 Answers

  1. As a general rule, I wouldn't use a function parameter as a local processing variable, i.e. I treat function parameters as read-only.

    In my mind, intuitively understandabie code is paramount for maintainability, and modifying a function parameter to use as a local processing variable tends to run counter to that goal. I have come to expect that a parameter will have the same value in the middle and bottom of a method as it does at the top. Plus, an aptly-named local processing variable may improve understandability.

    Still, as @Stewart says, this rule is more or less important depending on the length and complexity of the function. For short simple functions like the one you show, simply using the parameter itself may be easier to understand than introducing a new local variable (very subjective).

    Nevertheless, if I were to write something as simple as countDigits(), I'd tend to use a remainingBalance local processing variable in lieu of modifying the num parameter as part of local processing - just seems clearer to me.

  2. Sometimes, I will modify a local parameter at the beginning of a method to normalize the parameter:

    void saveName(String name) {
      name = (name != null ? name.trim() : "");
      ...
    }
    

    I rationalize that this is okay because:

    a. it is easy to see at the top of the method,

    b. the parameter maintains its the original conceptual intent, and

    c. the parameter is stable for the rest of the method

    Then again, half the time, I'm just as apt to use a local variable anyway, just to get a couple of extra finals in there (okay, that's a bad reason, but I like final):

    void saveName(final String name) {
      final String normalizedName = (name != null ? name.trim() : "");
      ...
    }
    
  3. If, 99% of the time, the code leaves function parameters unmodified (i.e. mutating parameters are unintuitive or unexpected for this code base) , then, during that other 1% of the time, dropping a quick comment about a mutating parameter at the top of a long/complex function could be a big boon to understandability:

    int CountDigits(int num) {
        // num is consumed
        int count = 1;
        while (num >= 10) {
            count++;
            num /= 10;
        }
        return count;
    }
    

P.S. :-)
parameters vs arguments http://en.wikipedia.org/wiki/Parameter_(computer_science)#Parameters_and_arguments

These two terms are sometimes loosely used interchangeably; in particular, "argument" is sometimes used in place of "parameter". Nevertheless, there is a difference. Properly, parameters appear in procedure definitions; arguments appear in procedure calls.

So,

int foo(int bar)

bar is a parameter.

int x = 5
int y = foo(x)

The value of x is the argument for the bar parameter.

like image 163
Bert F Avatar answered Sep 28 '22 03:09

Bert F