How do you guys decide between keeping track of something locally and then just passing it in to every method you call on it, or declaring an instance variable and using it in the methods?
你们如何决定在本地跟踪某些东西,然后将它传递给你调用它的每个方法,或者声明一个实例变量并在方法中使用它?
I tend to prefer instance variables kept in a list at the end of the Class. But as my programs become more and more complicated, this list gets longer and longer... I figure that if something is getting passed often enough it should just be visible to all the boys and girls who need it, but then I start wondering, "why not just make everything public! Then there will be no more need of passing anything at all!"
我倾向于选择保存在类末尾的列表中的实例变量。但是随着我的程序变得越来越复杂,这个列表变得越来越长......我认为如果某些事情经常被过去,那么所有需要它的男孩和女孩都应该可以看到它,但后来我开始怀疑, “为什么不把一切都公之于众呢?那么根本不需要传递任何东西!”
5 个解决方案
#1
42
Since you're referring to instance variables, I'm assuming that you're working in an object-oriented language. To some degree, when to use instance variables, how to define their scope, and when to use local variables is subjective, but there are a couple of rules of thumb you can follow whenever creating your classes.
因为你指的是实例变量,所以我假设你正在使用面向对象的语言。在某种程度上,何时使用实例变量,如何定义其范围以及何时使用局部变量是主观的,但是在创建类时,您可以遵循一些经验法则。
-
Instance variables are typically considered to be attributes of a class. Think of these as adjectives of the object that will be created from your class. If your instance data can be used to help describe the object, then it's probably safe to bet it's a good choice for instance data.
实例变量通常被认为是类的属性。将这些视为将从您的班级创建的对象的形容词。如果你的实例数据可以用来帮助描述对象,那么打赌它是一个很好的选择实例数据可能是安全的。
-
Local variables are used within the scope of methods to help them complete their work. Usually, a method should have a purpose of getting some data, returning some data, and/or proccessing/running an algorithm on some data. Sometimes, it helps to think of local variables as ways of helping a method get from beginning to end.
局部变量在方法范围内使用,以帮助他们完成工作。通常,方法应该具有获取某些数据,返回一些数据和/或在某些数据上处理/运行算法的目的。有时候,将局部变量视为帮助方法从头到尾的方法是有帮助的。
-
Instance variable scope is not just for security, but for encapsulation, as well. Don't assume that the "goal should be to keep all variables private." In cases of inheritance, making variables as protected is usually a good alternative. Rather than marking all instance data public, you create getters/setters for those that need to be accessed to the outside world. Don't make them all available - only the ones you need. This will come throughout the development lifecycle - it's hard to guess from the get go.
实例变量范围不仅用于安全性,还用于封装。不要认为“目标应该是保持所有变量的私密性”。在继承的情况下,将变量设置为受保护通常是一个很好的选择。您可以为需要访问外部世界的人创建getter / setter,而不是将所有实例数据公开。不要让它们全部可用 - 只有你需要的。这将贯穿整个开发生命周期 - 从开始就很难猜到。
When it comes to passing data around a class, it's difficult to say what you're doing is good practice without seeing some code . Sometimes, operating directly on the instance data is fine; other times, it's not. In my opinion, this is something that comes with experience - you'll develop some intuition as your object-oriented thinking skills improve.
当谈到在类中传递数据时,很难说你正在做的是没有看到一些代码的好习惯。有时,直接在实例数据上操作很好;其他时候,事实并非如此。在我看来,这是经验带来的东西 - 随着面向对象的思维技能的提高,你会发展出一些直觉。
#2
35
Mainly this depends on the lifetime of the data you store in the variable. If the data is only used during a computation, pass it as a parameter. If the data is bound to the lifetime of the object use an instance variable.
这主要取决于您存储在变量中的数据的生命周期。如果数据仅在计算期间使用,则将其作为参数传递。如果数据绑定到对象的生命周期,则使用实例变量。
When your list of variables gets too long, maybe it's a good point to think about refactoring some parts of the class into a new class.
当你的变量列表变得太长时,考虑将类的某些部分重构为新类可能是一个好点。
#3
14
In my opinion, instance variables are only necessary when the data will be used across calls.
在我看来,只有在跨越调用使用数据时才需要实例变量。
Here's an example:
这是一个例子:
myCircle = myDrawing.drawCircle(center, radius);
Now lets imaging the myDrawing class uses 15 helper functions to create the myCircle object and each of those functions will need the center and the radius. They should still not be set as instance variables of the myDrawing class. Because they will never be needed again.
现在让myDrawing类成像使用15个辅助函数来创建myCircle对象,每个函数都需要中心和半径。它们仍然不应被设置为myDrawing类的实例变量。因为永远不会再需要它们了。
On the other hand, the myCircle class will need to store both the center and radius as instance variables.
另一方面,myCircle类需要将center和radius存储为实例变量。
myCircle.move(newCenter);
myCircle.resize(newRadius);
In order for the myCircle object to know what it's radius and center are when these new calls are made, they need to be stored as instance variables, not just passed to the functions that need them.
为了让myCircle对象知道在进行这些新调用时它的半径和中心是什么,它们需要存储为实例变量,而不仅仅是传递给需要它们的函数。
So basically, instance variables are a way to save the "state" of an object. If a variable is not necessary to know the state of an object, then it shouldn't be an instance variable.
所以基本上,实例变量是一种保存对象“状态”的方法。如果不需要变量来知道对象的状态,那么它不应该是实例变量。
And as for making everything public. It might make your life easier in the moment. But it will come back to haunt you. Pease don't.
至于把一切都公之于众。它可能会让你的生活更轻松。但它会回来困扰你。皮斯没有。
#4
4
IMHO:
If the variable forms part of the state of the instance, then it should be an instance variable - classinstance HAS-A instancevariable.
如果变量构成实例状态的一部分,那么它应该是一个实例变量 - classinstance HAS-A instancevariable。
If I found myself passing something repeatedly into an instance's methods, or I found that I had a large number of instance variables I'd probably try and look at my design in case I'd missed something or made a bad abstraction somewhere.
如果我发现自己经常将某些东西传递给实例的方法,或者我发现我有大量的实例变量,我可能会尝试看看我的设计,以防我错过某些东西或者在某处做了一个糟糕的抽象。
Hope it helps
希望能帮助到你
#5
3
Of course it is easy to keep one big list of public variables in the class. But even intuitively, you can tell that this is not the way to go.
当然,在课堂上保留一大堆公共变量很容易。但即便如此,你可以说这不是可行的方法。
Define each variable right before you are going to use it. If a variable supports the function of a specific method, use it only in the scope of the method.
在您要使用它之前定义每个变量。如果变量支持特定方法的功能,则仅在方法范围内使用它。
Also think about security, a public class variable is susceptible to unwanted changes from "outside" code. Your main goal should be to keep all variables private, and any variable which is not, should have a very good reason to be so.
另外考虑安全性,公共类变量容易受到“外部”代码的不必要更改。你的主要目标应该是保持所有变量的私有性,而任何不变量的变量都应该有一个很好的理由。
About passing parameters all they way up the stack, this can get ugly very fast. A rule of thumb is to keep your method signatures clean and elegant. If you see many methods using the same data, decide either if it's important enough to be a class member, and if it's not, refactor your code to have it make more sense.
关于将参数传递到堆栈中,这可能会非常快速地变得难看。一个经验法则是保持您的方法签名干净和优雅。如果您看到许多方法使用相同的数据,请确定它是否足够重要成为类成员,如果不是,请重构代码以使其更有意义。
It boils down to common sense. Think exactly where and why you are declaring each new variable, what it's function should be, and from there make a decision regarding which scope it should live in.
它归结为常识。仔细考虑你在哪里以及为什么要声明每个新变量,它的功能应该是什么,并从那里决定它应该适用于哪个范围。
#1
42
Since you're referring to instance variables, I'm assuming that you're working in an object-oriented language. To some degree, when to use instance variables, how to define their scope, and when to use local variables is subjective, but there are a couple of rules of thumb you can follow whenever creating your classes.
因为你指的是实例变量,所以我假设你正在使用面向对象的语言。在某种程度上,何时使用实例变量,如何定义其范围以及何时使用局部变量是主观的,但是在创建类时,您可以遵循一些经验法则。
-
Instance variables are typically considered to be attributes of a class. Think of these as adjectives of the object that will be created from your class. If your instance data can be used to help describe the object, then it's probably safe to bet it's a good choice for instance data.
实例变量通常被认为是类的属性。将这些视为将从您的班级创建的对象的形容词。如果你的实例数据可以用来帮助描述对象,那么打赌它是一个很好的选择实例数据可能是安全的。
-
Local variables are used within the scope of methods to help them complete their work. Usually, a method should have a purpose of getting some data, returning some data, and/or proccessing/running an algorithm on some data. Sometimes, it helps to think of local variables as ways of helping a method get from beginning to end.
局部变量在方法范围内使用,以帮助他们完成工作。通常,方法应该具有获取某些数据,返回一些数据和/或在某些数据上处理/运行算法的目的。有时候,将局部变量视为帮助方法从头到尾的方法是有帮助的。
-
Instance variable scope is not just for security, but for encapsulation, as well. Don't assume that the "goal should be to keep all variables private." In cases of inheritance, making variables as protected is usually a good alternative. Rather than marking all instance data public, you create getters/setters for those that need to be accessed to the outside world. Don't make them all available - only the ones you need. This will come throughout the development lifecycle - it's hard to guess from the get go.
实例变量范围不仅用于安全性,还用于封装。不要认为“目标应该是保持所有变量的私密性”。在继承的情况下,将变量设置为受保护通常是一个很好的选择。您可以为需要访问外部世界的人创建getter / setter,而不是将所有实例数据公开。不要让它们全部可用 - 只有你需要的。这将贯穿整个开发生命周期 - 从开始就很难猜到。
When it comes to passing data around a class, it's difficult to say what you're doing is good practice without seeing some code . Sometimes, operating directly on the instance data is fine; other times, it's not. In my opinion, this is something that comes with experience - you'll develop some intuition as your object-oriented thinking skills improve.
当谈到在类中传递数据时,很难说你正在做的是没有看到一些代码的好习惯。有时,直接在实例数据上操作很好;其他时候,事实并非如此。在我看来,这是经验带来的东西 - 随着面向对象的思维技能的提高,你会发展出一些直觉。
#2
35
Mainly this depends on the lifetime of the data you store in the variable. If the data is only used during a computation, pass it as a parameter. If the data is bound to the lifetime of the object use an instance variable.
这主要取决于您存储在变量中的数据的生命周期。如果数据仅在计算期间使用,则将其作为参数传递。如果数据绑定到对象的生命周期,则使用实例变量。
When your list of variables gets too long, maybe it's a good point to think about refactoring some parts of the class into a new class.
当你的变量列表变得太长时,考虑将类的某些部分重构为新类可能是一个好点。
#3
14
In my opinion, instance variables are only necessary when the data will be used across calls.
在我看来,只有在跨越调用使用数据时才需要实例变量。
Here's an example:
这是一个例子:
myCircle = myDrawing.drawCircle(center, radius);
Now lets imaging the myDrawing class uses 15 helper functions to create the myCircle object and each of those functions will need the center and the radius. They should still not be set as instance variables of the myDrawing class. Because they will never be needed again.
现在让myDrawing类成像使用15个辅助函数来创建myCircle对象,每个函数都需要中心和半径。它们仍然不应被设置为myDrawing类的实例变量。因为永远不会再需要它们了。
On the other hand, the myCircle class will need to store both the center and radius as instance variables.
另一方面,myCircle类需要将center和radius存储为实例变量。
myCircle.move(newCenter);
myCircle.resize(newRadius);
In order for the myCircle object to know what it's radius and center are when these new calls are made, they need to be stored as instance variables, not just passed to the functions that need them.
为了让myCircle对象知道在进行这些新调用时它的半径和中心是什么,它们需要存储为实例变量,而不仅仅是传递给需要它们的函数。
So basically, instance variables are a way to save the "state" of an object. If a variable is not necessary to know the state of an object, then it shouldn't be an instance variable.
所以基本上,实例变量是一种保存对象“状态”的方法。如果不需要变量来知道对象的状态,那么它不应该是实例变量。
And as for making everything public. It might make your life easier in the moment. But it will come back to haunt you. Pease don't.
至于把一切都公之于众。它可能会让你的生活更轻松。但它会回来困扰你。皮斯没有。
#4
4
IMHO:
If the variable forms part of the state of the instance, then it should be an instance variable - classinstance HAS-A instancevariable.
如果变量构成实例状态的一部分,那么它应该是一个实例变量 - classinstance HAS-A instancevariable。
If I found myself passing something repeatedly into an instance's methods, or I found that I had a large number of instance variables I'd probably try and look at my design in case I'd missed something or made a bad abstraction somewhere.
如果我发现自己经常将某些东西传递给实例的方法,或者我发现我有大量的实例变量,我可能会尝试看看我的设计,以防我错过某些东西或者在某处做了一个糟糕的抽象。
Hope it helps
希望能帮助到你
#5
3
Of course it is easy to keep one big list of public variables in the class. But even intuitively, you can tell that this is not the way to go.
当然,在课堂上保留一大堆公共变量很容易。但即便如此,你可以说这不是可行的方法。
Define each variable right before you are going to use it. If a variable supports the function of a specific method, use it only in the scope of the method.
在您要使用它之前定义每个变量。如果变量支持特定方法的功能,则仅在方法范围内使用它。
Also think about security, a public class variable is susceptible to unwanted changes from "outside" code. Your main goal should be to keep all variables private, and any variable which is not, should have a very good reason to be so.
另外考虑安全性,公共类变量容易受到“外部”代码的不必要更改。你的主要目标应该是保持所有变量的私有性,而任何不变量的变量都应该有一个很好的理由。
About passing parameters all they way up the stack, this can get ugly very fast. A rule of thumb is to keep your method signatures clean and elegant. If you see many methods using the same data, decide either if it's important enough to be a class member, and if it's not, refactor your code to have it make more sense.
关于将参数传递到堆栈中,这可能会非常快速地变得难看。一个经验法则是保持您的方法签名干净和优雅。如果您看到许多方法使用相同的数据,请确定它是否足够重要成为类成员,如果不是,请重构代码以使其更有意义。
It boils down to common sense. Think exactly where and why you are declaring each new variable, what it's function should be, and from there make a decision regarding which scope it should live in.
它归结为常识。仔细考虑你在哪里以及为什么要声明每个新变量,它的功能应该是什么,并从那里决定它应该适用于哪个范围。