Using function arguments as local variables

前端 未结 9 1298
野的像风
野的像风 2020-12-30 03:57

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

int CountDigits(int num) {
    int count = 1;
             


        
相关标签:
9条回答
  • 2020-12-30 04:08

    If I don't need a copy of the original value, I don't declare a new variable.

    IMO I don't think mutating the parameter values is a bad practice in general,
    it depends on how you're going to use it in your code.

    0 讨论(0)
  • 2020-12-30 04:09

    About C and C++:

    My opinion is that using the parameter as a local variable of the function is fine because it is a local variable already. Why then not use it as such?

    I feel silly too when copying the parameter into a new local variable just to have a modifiable variable to work with.

    But I think this is pretty much a personal opinion. Do it as you like. If you feel sill copying the parameter just because of this, it indicates your personality doesn't like it and then you shouldn't do it.

    0 讨论(0)
  • 2020-12-30 04:14

    I typically don't modify function parameters, unless they're pointers, in which case I might alter the value that's pointed to.

    0 讨论(0)
  • 2020-12-30 04:20

    The code needs to be as self sufficient as possible. What I mean by that is you now have a dependency on what is being passed in as part of your algorithm. If another member of your team decides to change this to a pass by reference then you might have big problems.

    The best practice is definitely to copy the inbound parameters if you expect them to be immutable.

    0 讨论(0)
  • 2020-12-30 04:22

    I would generally not change the parameter value within the function. If at some point later in the function you need to refer to the original value, you still have it. in your simple case, there is no problem, but if you add more code later, you may refer to 'num' without realizing it has been changed.

    0 讨论(0)
  • 2020-12-30 04:27

    I think the best-practices of this varies by language. For example, in Perl you can localize any variable or even part of a variable to a local scope, so that changing it in that scope will not have any affect outside of it:

    sub my_function
    {
        my ($arg1, $arg2) = @_;    # get the local variables off the stack
    
        local $arg1;    # changing $arg1 here will not be visible outside this scope
        $arg1++;
    
        local $arg2->{key1};   # only the key1 portion of the hashref referenced by $arg2 is localized
        $arg2->{key1}->{key2} = 'foo';   # this change is not visible outside the function
    
    }
    

    Occasionally I have been bitten by forgetting to localize a data structure that was passed by reference to a function, that I changed inside the function. Conversely, I have also returned a data structure as a function result that was shared among multiple systems and the caller then proceeded to change the data by mistake, affecting these other systems in a difficult-to-trace problem usually called action at a distance. The best thing to do here would be to make a clone of the data before returning it*, or make it read-only**.

    * In Perl, see the function dclone() in the built-in Storable module.
    ** In Perl, see lock_hash() or lock_hash_ref() in the built-in Hash::Util module).

    0 讨论(0)
提交回复
热议问题