使用函数参数作为局部变量 [英] Using function arguments as local variables

查看:239
本文介绍了使用函数参数作为局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样的事情(是的,这不涉及某些极端情况-这不是重点):

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;
}

您对此有何看法?也就是说,使用函数参数作为局部变量.
两者都放在堆栈上,并且在性能上几乎相同,我想知道它的最佳实践方面.
当我在由int numCopy = num组成的函数中添加一条额外且非常多余的行时,我感觉像个白痴,但是它确实使我感到烦恼.
你怎么认为?应该避免这种情况吗?

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?

推荐答案

  1. 通常,我不会将函数参数用作本地处理变量,即,我将函数参数视为只读.

  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.

在我看来,直观地理解代码对于保持可维护性至关重要,并且修改功能参数以用作本地处理变量 tend 以与该目标背道而驰.我已经期望参数在方法的中间和底部与在顶部具有相同的值. 另外,适当命名的本地处理变量可以提高易懂性.

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.

仍然,如@Stewart所说,此规则或多或少地取决于函数的长度和复杂性.对于您展示的简短功能,简单地使用参数本身可能比引入新的局部变量(非常主观)更容易理解.

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).

尽管如此,如果我要编写像countDigits()这样简单的内容,我倾向于使用remainingBalance本地处理变量,而不是将num参数修改为本地处理的一部分-似乎更清楚了我.

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.

有时,我会在方法的开头修改本地参数以规范该参数:

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.在该方法的顶部很容易看到,

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

b.该参数保持其原始的概念意图,并且

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

c.该参数对于其余方法都是稳定的

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

然后再次,有一半时间,我仍然倾向于使用局部变量,只是在那里获得了几个额外的final(好吧,这是一个不好的原因,但是我喜欢final ):

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() : "");
  ...
}

  • 如果在99%的时间内,代码未修改函数参数(即该代码库的变异参数是 unintuitive unexpected ),则,在那另外1%的时间内,在长/复杂函数的顶部添加关于变异参数的快速注释可能会大大提高理解力:

  • 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. :-)
    参数与参数 http://en.wikipedia.org/wiki/Parameter_(computer_science)#Parameters_and_arguments


    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.

    所以

    int foo(int bar)
    

    bar是参数.

    int x = 5
    int y = foo(x)
    

    x的值是bar参数的参数.

    这篇关于使用函数参数作为局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆