使用类变量与将局部变量发送到函数/方法

发布于 2024-10-30 05:11:54 字数 584 浏览 1 评论 0原文

什么时候将局部变量作为参数推送到函数/方法而不是使用类变量代替函数/方法变量是一种好的形式。

例如,我可以有一个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉默的熊 2024-11-06 05:11:54

对于任何一般情况,唯一的突然答案是:这取决于您的具体情况。数据成员、静态成员和函数参数都有不同的用途。当然,我们可以提供一些关键提示,告诉您应该寻找哪些类型的标志来选择其中之一。

典型情况:

  • 数据成员:该值是对象(如在类的实例中)状态的一部分。您希望对方法的其他调用能够反映此特定状态。
  • 静态成员:该值对于该类的所有实例具有同时、相同的含义。这通常仅用于常量(即使在运行时初始化,如单例),但在某些情况下,需要可变的类状态。
  • 函数参数:该值仅对函数/方法的特定执行有意义。从一次调用到下一次调用,该值可能会发生变化。

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

考虑以下问题:

  • 无论从何处调用方法,您总是将相同的值传递给方法吗?考虑使参数成为常量并将参数隐藏起来。考虑定义重载:一种不带参数的常见情况,一种带参数的灵活性。
  • 每次调用函数时都需要设置一个数据成员(通过 setter)吗?考虑将该值作为函数的参数。如果您需要用两行替换每个调用来预先设置值,则无需保存函数签名。

我的印象是您和您的同事对此参数的性质存在简单的误解。确保您清楚地理解同事的论点并表达清楚。尝试重新表述您想说的内容。

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:

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

Consider the following questions:

  • 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'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.

紫﹏色ふ单纯 2024-11-06 05:11:54

我从依赖的角度来看待它,即谁依赖于变量(在你的例子中是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 only DoSomething() requires var, then DoSomething(int var) is essential.

苏佲洛 2024-11-06 05:11:54

它不应该是关于什么提供了更多或更少的代码或者什么需要更多的努力来输入。它是关于在该类和函数的上下文中什么更符合逻辑。像您一样保持事物彼此隔离通常是一件好事,但不要做得太过分。当函数的目的很清楚它应该处理类 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文