Java变量声明
好吧,这可能是基本的,但我想要好的程序员对此的意见。
处理小类文件中的变量的好方法是什么?
我喜欢模块化方法并保留做真正特定事情的方法。 我最终在这些方法之间传递变量。将多个方法中使用的变量作为成员变量是一个好习惯吗?或者将变量传递给方法更好?
例如:
class Test{
somefunction(int a, int b, int c, int d) {
doSomething(a, b, c);
doOneMoreThing(a, c, d);
}
void doSomething(int a, int b, int c) { }
void doOneMoreThing(int a, int c, int d) { }
}
在上面的例子中,你认为变量应该保留为成员变量吗? 请解释为什么一种方法优于另一种。
Ok, this might be basic, but I want good programmers' opinions on this.
What is a good way to handle variables in a small class file?
I like modularizing methods and keeping methods that do really specific things.
I end up passing variables between those methods. Is it a good practice to make variables used across many methods as member variables? Or is it better to pass the variables to methods?
For example:
class Test{
somefunction(int a, int b, int c, int d) {
doSomething(a, b, c);
doOneMoreThing(a, c, d);
}
void doSomething(int a, int b, int c) { }
void doOneMoreThing(int a, int c, int d) { }
}
In the above example, do you think the variables should be kept as member variables?
Please explain why one methodology is preferred over the other.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您不关心对象的状态,那么将变量传递给方法就可以了。在这种情况下,我会在方法上使用
static
修饰符,然后您不必实例化该类,并且可以像这样调用该方法:If you don't care about the state of the object, then passing the variables to the method is fine. In that case, I would use a
static
modifier on the method, then you don't have to instansiate the class and you can call the method like so:如果要重用这些变量,则可以将它们声明为类变量。如果不是,那么它们应该是在各自方法中定义的局部变量。
If you are going to reuse the variables, then you can declare them as class variables. If not, then they should be local variables defined in their respective methods.
首先,
Somefunction(... }
是一个语法错误。其次,方法名称应该以小写字母开头,类名称应该以大写字母开头。第三,我们不知道什么是最好的方式是不知道这些方法的作用、用途以及参数来自哪里First of all
Somefunction(... }
is a syntax error. Second, method names should start with lower case letters, and class names should start with upper case. Third, we have no idea what the best way is without knowing what these methods do, what they're used for, and where their parameters come from取决于您需要更改代码的频率(或者您应该在设计时考虑更改代码的频率)。如果签名发生变化,您必须在很多地方进行更改。这意味着即使您重构签名,也需要测试更多代码。我会在创建成员变量和封装方面犯错误。
Depends on how often you have to change your code (or you should think about how often you're going to change it when you design it). If the signature changes you have to change it in a lot of places. That means more code to test even when you refactor the signature. I would err on the side of creating member variables and encapsulating.
您是否需要在方法调用之间保留变量并重用它们的值?如果是这样,他们就应该是班级成员。 (至少在某些类别中 - 不一定是这个类别。)
否则,这在某种程度上是一个品味问题。一个重要因素是,局部变量不会向对象添加状态,但如果同时使用,这会很有用。将所有变量保持在本地甚至可以使您的类不可变,这通常使其自动成为线程安全的。但即使在单线程环境中,不可变类也更容易理解和维护。
OTOH 传递大量参数可能会很尴尬。您可以考虑引入参数对象来缓解此问题。
Do you need to keep the variables around between method calls, and reuse their value? If so, they should be class members. (At least in some class - not necessarily this one.)
Otherwise it is somewhat a matter of taste. One important factor is though that local variables don't add state to the object, which can be useful if it is used concurrently. Keeping all variables local may even allow you to make your class immutable, which usually makes it automatically thread-safe. But even in a single-threaded environment an immutable class is easier to understand and maintain.
OTOH passing lots of parameters around can be awkward. You may consider introducing a Parameter Object to alleviate this problem.
如果我有一些变量最终会传递给一堆私有方法,我通常会将它们移动到私有内部工作类中。
相反,
我会将永远不会改变的变量移至工作人员的属性中。
这使得审阅者清楚每个范围内哪些内容在该范围内是不变的。
If I have some variables that I would end up passing to a bunch of private methods, I'll often move them into a private inner worker class.
Instead of
I'll move the variables that never difference into properties on a worker.
This makes it clear to a reviewer what in each scope is invariant within that scope.
成员变量应该存在来维护类中的某种状态。如果你的类维护状态,那么一定要为你需要跟踪的那些东西定义成员变量。如果一个类不维护状态,那么就没有理由让事物成为成员(我不得不调试遗留代码,其中变量不应该成为成员,但在多次调用对象时会导致错误,因为状态是不可预测)。
然而,虽然您可能喜欢“模块化”功能,但请阅读耦合与内聚的关系。在类中具有过多的功能但较少的依赖项与具有很少但高度特定的功能和大量的依赖项之间需要取得平衡。
Member variables should exist to maintain some kind of state in a class. If your class maintains state then definitely define member variables for those things you need to track. If a class does not maintain state then there is no reason to make things members (I have had to debug legacy code where variables should not have been made members but were and it was causing errors when making multiple calls to the object because the state was unpredictable).
However, while you might like "modularizing" functionality, read up on coupling vs. cohesion. There is a balance to be struck between having too much functionality in a class but fewer dependencies and having very little but highly specific functionality and lots of dependencies.
拥有无用的成员变量通常被认为是糟糕的设计。
但是,如果您在许多方法中使用多个变量集,则可以(并且应该)将多个变量集组合到一个新类中。
Having useless member variables is usually regarded to as bad design.
But you can (and should) combine multiple variable sets into a new class if you use that variables in lots of methods.