使用类变量与将局部变量发送到函数/方法
什么时候将局部变量作为参数推送到函数/方法而不是使用类变量代替函数/方法变量是一种好的形式。
例如,我可以有一个函数:
int DoSomething(int var)
{
if(var == -1)
return 0;
}
或者我可以有一个类变量“_var”并在同一个函数中使用它,如下所示:
int DoSomething()
{
if(_var == -1)
return 0;
}
如果我们有一个要在某个函数中使用的类变量,我愿意/method,在上面的示例中称为 DoSomething
,我应该向 DoSomething
函数/方法发送类变量作为参数,以便该函数更易于阅读和测试。
什么时候做这两者比较好?我知道这是一个沉重的问题,但我正在尝试与同事论证我的论点,他们表示我会向函数/方法签名添加更多代码,而不是保留函数/方法签名更小。
在我看来,我通过将类变量推送到相应的函数/方法来使代码更清晰、更易于维护,而不是强迫它们依赖/了解类变量的存在。
请指教。
When is it good form to push a local variable to a function/method as a parameter, rather than using a class variable in place of the function/method variable.
For instance, I can have a function:
int DoSomething(int var)
{
if(var == -1)
return 0;
}
or I can have a class variable "_var" and use it in the same function, like this:
int DoSomething()
{
if(_var == -1)
return 0;
}
I'm of the mind to, if we have a class variable to be used in some function/method, called DoSomething
in my example above, that I should send the DoSomething
function/method the class variable as a parameter, so that the function is easier to read and test.
When is it good form to do either? I know this is a loaded question, but I'm trying to make a case for my argument with a co-worker, and they're stating that I would add more code to the function/method signatures, rather than keeping the function/method signatures smaller.
In my mind, I'm making the code cleaner and easier to maintain by pushing the class variable(s) to the respective functions/methods, rather than forcing them to rely on/know about a class variable's existence.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于任何一般情况,唯一的突然答案是:这取决于您的具体情况。数据成员、静态成员和函数参数都有不同的用途。当然,我们可以提供一些关键提示,告诉您应该寻找哪些类型的标志来选择其中之一。
典型情况:
错误的选择有一些常见的症状。
考虑以下问题:
我的印象是您和您的同事对此参数的性质存在简单的误解。确保您清楚地理解同事的论点并表达清楚。尝试重新表述您想说的内容。
The only answer out of the blue, for a any generic case is: it depends on your specific case. Data members, static members and function arguments all serve different purposes. Of course, there are some key tips we can give for what types of signs you should look for choosing one or the other.
Typical cases:
There are some common symptoms of bad choice.
Consider the following questions:
I'm under the impression that you and your co-worker are in a simple misunderstanding of the nature of this parameter. Make sure you clearly understand your co-worker's arguments and make yourself clear. Try to rephrase what it is that you're trying to say.
我从依赖的角度来看待它,即谁依赖于变量(在你的例子中是
var
),它是一个方法还是一个类?例如,JavaBeans 具有类依赖的类变量,因此如果类需要这些变量,那么
DoSomething()
是最好的。或者,如果您的类不关心
var
并且在其他任何地方都不需要它,并且只有DoSomething()
需要var
,那么DoSomething(int var)
是必不可少的。I look at it in terms of dependency, i.e. who is dependent on the variable (in your case
var
), is it a method or a class?For e.g. JavaBeans have class variables that are dependent by the class, so if the class needs these variables then
DoSomething()
is best.Alternatively, if you class doesn't care about
var
and doesn't need it anywhere else, and onlyDoSomething()
requiresvar
, thenDoSomething(int var)
is essential.它不应该是关于什么提供了更多或更少的代码或者什么需要更多的努力来输入。它是关于在该类和函数的上下文中什么更符合逻辑。像您一样保持事物彼此隔离通常是一件好事,但不要做得太过分。当函数的目的很清楚它应该处理类 var 中包含的值时,它应该这样做,而不是通过参数接收值。
It shouldn't be about what gives more or less code or what takes more effort to type. It is about what is more logical within the context of that class and function. Keeping things isolated from each other like you do is in general a good thing but don't over do it. When it is clear from the purpose of a function that it should be working on a value contained in a class var it should do so and not receive the value through a parameter.