使用类变量与将本地变量发送到函数/方法 [英] Using a Class Variable vs Sending Local Variable to Functions/Methods

查看:80
本文介绍了使用类变量与将本地变量发送到函数/方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

何时将局部变量作为参数推送到函数/方法,而不是使用类变量代替函数/方法变量。

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

或者我可以有一个类变量_var函数,像这样:

or I can have a class variable "_var" and use it in the same function, like this:

int DoSomething()
{
   if(_var == -1)
     return 0;
}



我很想,如果我们有一个类变量用于某些函数/方法,在上面的例子中调用 DoSomething ,我应该发送 DoSomething 类变量作为参数,以便函数更容易阅读和测试。

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.

请指教。

推荐答案

唯一的答案是蓝色的,对于任何一般的情况是:这取决于你的具体情况下。数据成员,静态成员和函数参数都具有不同的用途。 当然,有一些关键的提示,我们可以给你应该寻找什么类型的迹象,选择一个或另一个。

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.

典型案例:


  • 数据成员:该值是对象(类的实例)状态的一部分。

  • 静态成员:该值对所有实例具有同时,完全相同的含义的类。这通常只用于常量(即使在运行时初始化,如单例),但在某些情况下,需要可变类的状态。

  • >:该值仅对函数/方法的特定执行有意义。

  • Data member: the value is part of the object's (as in instance of a class) state. You want other calls to methods to reflect this particular state.
  • Static member: the value has simultaneous, identical meaning to all instances of the class. This is typically used only for constants (even when initialized at runtime, like a singleton) but in some cases, there is need for mutable class state.
  • Function argument: the value has meaning only for a specific execution of the function/method. This value is subject to change from one invocation to the next.

有一些常见的错误选择症状。

There are some common symptoms of bad choice.

请考虑以下问题:


  • 总是将相同的值传递给方法,无论你从哪里调用它?考虑使参数是一个常量,隐藏参数。考虑定义重载:一个没有参数的常见情况和一个参数的灵活性。

  • 你需要设置一个数据成员函数?考虑将值作为函数的参数。如果您需要用两行代替每个调用,以便手动设置值,则无需保存函数签名。

  • Do you always pass the same value to a method, no matter where you call it from? Consider making the argument a constant and hiding the argument away. Consider defining overloads: one without argument for the common case and one with argument for flexibility.
  • Do you need to set a data member (via a setter) every single time you invoke the function? Consider making the value an argument to the function. There's no need to save on the function signature if you need to replace each call with two lines to set the value before hand.

I因为你和你的同事对这个参数的性质有一个简单的误解。确保你清楚地了解你的同事的论点,并清楚说明。尝试重新说明您要说的是什么。

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.

这篇关于使用类变量与将本地变量发送到函数/方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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