使用常量还是类变量?
我在页面上显示了许多导航链接。 它将在应用程序中修复,但将来可能会更改,但不是交互式的,只要新版本更改了它。因此在使用的应用程序中固定。
链接的数量被设置为在控制器中的许多不同操作中使用。
我应该使用类级别变量 @@number_of_link
s 使其保留在类内并允许同名的其他用途(不同的控制器和模型)而不发生冲突(即作用域),还是应该使用像 NumberOfLinks
这样的常量?
我已经得到了关于这两个方面的建议,并且想确认我对第一个的偏好。
I am showing a number of navigation links on a page.
It will be fixed in the application but in the future could change, but not interactively, just if a new release changed it. So fixed within the application in use.
The number of links is set for use within many different actions in the controller.
Should I use a class level variable @@number_of_link
s to make it stay within the class and allow other uses (different controllers and models say) of the same name without conflict (i.e. scoped), or should I use a constant like NumberOfLinks
?
I've been given advice on both and would like to confirm my preference for the first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除非它是常数,否则它绝对不是常数!
我建议使用类实例变量,而不是类变量。您可以获得类变量的所有好处,甚至具有更好的作用域(类的其他成员不会破坏该值,并且也更容易访问。
请参阅 http://blog.codegram.com/2011/4/understanding-class-instance-variables-in-ruby了解更多。
It's definitely not a constant unless it's constant!
Instead of a class variable, I'd suggest a class-instance variable. You get all the benefits of a class variable, with even better scoping (other members of the class won't clobber the value, and easier access too.
see http://blog.codegram.com/2011/4/understanding-class-instance-variables-in-ruby for more.
如果它仅随着软件的新版本而改变,则将其设为常量。这更好地反映了该值的意图。
不会被应用程序逻辑更改的值不需要是瞬态变量。我尽可能避免类变量(和类实例变量),因为许多开发人员很难理解两者之间的区别、何时使用它们等。
If it will only be changed with a new release of the software then make it a constant. That better reflects the intent of that value.
Values that won't be changed by application logic do not need to be transient variables. I avoid class variables (and class instance variables) when possible because many developers have trouble understanding the difference between the two, when to use them, etc.
就像伊恩说的那样..使用类实例变量..但是你可以使用 attr_accessor 减少代码
Like Iain says.. use class instance variables.. but you can reduce that code using attr_accessor
如果它具有严格的性质,您不打算使用应用程序逻辑进行修改,则应该使用常量。
如果您希望随着类的状态而改变(如果它是根据数据库交互的状态推断出来的),您应该使用类变量。
从性能角度来看,这并不重要,在这种情况下,按照上述方式使用它,您可以使用变量类型来假设意图,这使得贡献开发人员更容易理解而无需“解码”。
If it is something of a strict nature that you don't plan on modifying with application logic you should use a constant.
If it is something you expect to change with the state of the class (if it is extrapolated based on the state of a database interaction) you should use a class variable.
From a performance perspective it doesn't matter, in this case, using it the way described above you can assume the intent using the type of variable which makes it easier for contributing developers to understand without "decoding".
假设您需要更新链接数量,如果您对常量进行更新,则 ruby 会发出这样的警告:“警告:已初始化常量 NumOfLinks”,
在这种情况下,您应该坚持使用类变量。
let's say you need to update number of links if you do it over constant ruby will warn like this "warning: already initialized constant NumOfLinks"
in such case you should stick with class variable.