简体   繁体   English

在同一类中设置全局变量的例程的良好命名约定是什么?

[英]What is a good naming convention for a routine that sets a global variable in the same class

Code Complete (Chapter 7, Section 3) says that a good function should be named for the value it returns and a good procedure name in a class should be named for what it does. 代码完成(第7章,第3节)说,应该为它返回的值命名一个好的函数,而在类中应该为它的作用命名一个好的过程名称。

When I write synchronized methods in Delphi (pre 2009) I sometimes need to use them to set global variables, a seemingly bad programming practice, but a necessary once since I can't pass variables. 当我在Delphi(2009之前)中编写同步方法时,有时需要使用它们来设置全局变量,这似乎是不好的编程习惯,但由于无法传递变量,因此这是必须的。 I don't want to call them "Get" or "Set" because I use those for my property methods. 我不想将它们称为“获取”或“设置”,因为我将它们用于属性方法。

Anyone have a better naming convention for these? 有没有更好的命名约定?

I don't want to call them "Get" or "Set" because I use those for my property methods. 我不想将它们称为“获取”或“设置”,因为我将它们用于属性方法。

That seems like a pretty arbitrary decision. 这似乎是一个非常武断的决定。 Might you also say you don't want to use "set" on "setName" because you also used it on "setAge"? 您是否可能还说过不想在“ setName”上使用“ set”,因为您也在“ setAge”上使用了它?

That said, having a static with a setter is literally a public global variable ALA Basic-- Are you sure that's the only way to accomplish your mission? 就是说,带有setter的静态变量实际上是一个公共全局变量ALA Basic-您确定这是完成任务的唯一方法吗?

I'm not saying the static is absolutely wrong, but you should do your best to manipulate it in the object that defines it rather than having a setter, otherwise your exposing a lot of your object's internal state in a way that's going to be hard to control. 我并不是说静态绝对是错误的,但是您应该尽最大努力在定义它的对象中操作它,而不要使用setter,否则您将以一种很难的方式暴露对象的很多内部状态控制。

I'd say the advice from Code Complete is pretty strong and your objection "because I use those for my property methods" is pretty weak. 我想说来自Code Complete的建议非常有力,而您的反对“因为我将那些用于我的属性方法”是很弱的。 Those property setter/getters should be private anyway. 无论如何,这些财产设定者应该是私人的。 Consider it a form of overloading and call them SetFoo and GetFoo. 将其视为重载的一种形式,并将其称为SetFoo和GetFoo。

What Delphi version are you using? 您正在使用哪个Delphi版本? If using D2006 or 2007 you can move the globals into class properties with class methods to get and set the values. 如果使用D2006或2007,则可以使用类方法将全局变量移到类属性中,以获取和设置值。 As these are property getters and setters, using Get and Set are appropriate. 由于这些是属性获取器和设置器,因此使用Get和Set是合适的。

type
 TMyObject = class(TObject)
 private
    class var
      FStringProperty : string;

    class function GetStringProperty: String; static;
    class procedure SetStringProperty(const Value : string);static;
  public
    class property StringProperty : String read GetStringProperty write SetStringProperty;
  end;

Property getters and setters don't have names starting with get and set because it's some convention reserved for naming getters and setters. 属性获取器设置器没有以getset开头的名称,因为这是为命名获取器设置器保留的约定。 They have those names because that's what they do . 他们之所以有这些名字,是因为他们就是这样做的 Since your synchronized method's purpose is to set the value of a variable, it makes perfect sense to give it a "set" name. 由于同步方法的目的是设置变量的值,因此给它取一个“设置”名称是很有意义的。

You could choose a synonymous verb, like assign or copy , just to be different from set , but those are unconventional names for the purpose you've described. 您可以选择一个同义词动词(例如Assigncopy)来区别于set ,但出于您描述的目的,这些动词是非常规名称。 When you have a routine that sets the value of Foo , convention dictates that the function must be named SetFoo . 当您有一个设置Foo值的例程时,约定规定该函数必须命名为SetFoo Ultimately, I think you just need to get over whatever hangup you have about using get and set for things that aren't property accessors. 最终,我认为您只需要克服有关使用getset来处理非属性访问器的麻烦。

In my opinion writing to global variables should be easily distinguishable from normal setters. 在我看来,写全局变量应该很容易与普通的二传手区分开。 If a global variable cannot be avoided I normally use 如果无法避免使用全局变量,我通常会使用

SetGlobalFoo(...);

for this purpose. 以此目的。 The overhead for the long name is OK IMO because these constructs should be rarely used. 长名称的开销是可以的IMO,因为这些构造应很少使用。

I would use SetXXX and GetXXX for private and global variables because I don´t see a difference in what those methods do. 我将SetXXXGetXXX用于私有变量和全局变量,因为我看不到这些方法的区别。 The operation to SetXXX is a set over a data area. SetXXX的操作是对数据区域的设置。 If that data area is global, local or remote, it´s an internal detail of the method that should not be visible from outside. 如果该数据区域是全局的,本地的或远程的,则它是方法的内部细节,不应从外部看到。

The IDE will help you to know if that data area is local or not, but if you prefer, you can write a simple line of comment stating it. IDE将帮助您了解该数据区域是否是本地数据,但是如果您愿意,可以编写简单的注释行来说明它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM