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

时间:2022-05-06 03:37:09

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. I don't want to call them "Get" or "Set" because I use those for my property methods.

当我在Delphi中编写同步方法时(2009年之前),我有时需要使用它们来设置全局变量,这是一个看似糟糕的编程实践,但由于我无法传递变量,因此需要一次。我不想称它们为“Get”或“Set”,因为我将它们用于我的属性方法。

Anyone have a better naming convention for these?

任何人都有更好的命名约定?

6 个解决方案

#1


I don't want to call them "Get" or "Set" because I use those for my property methods.

我不想称它们为“Get”或“Set”,因为我将它们用于我的属性方法。

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,否则你会以一种很难的方式暴露你对象的很多内部状态控制。

#2


What Delphi version are you using? If using D2006 or 2007 you can move the globals into class properties with class methods to get and set the values. As these are property getters and setters, using Get and Set are appropriate.

您使用的是什么Delphi版本?如果使用D2006或2007,则可以使用类方法将全局变量移动到类属性中以获取和设置值。由于这些是属性getter和setter,因此使用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;

#3


Property getters and setters don't have names starting with get and set because it's some convention reserved for naming getters and setters. 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.

属性getter和setter没有以get和set开头的名称,因为它是为命名getter和setter而保留的一些约定。他们有这些名字因为这就是他们所做的。由于您的synchronized方法的目的是设置变量的值,因此给它一个“set”名称是完全合理的。

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. When you have a routine that sets the value of Foo, convention dictates that the function must be named 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.

您可以选择同义动词,例如分配或复制,只是为了与set不同,但这些是您描述的目的的非常规名称。当你有一个设置Foo值的例程时,约定规定该函数必须命名为SetFoo。最后,我认为你只需要克服你使用get和set为非属性访问器设置的任何挂断。

#4


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. Those property setter/getters should be private anyway. Consider it a form of overloading and call them SetFoo and GetFoo.

我会说Code Complete的建议相当强烈,你的反对意见“因为我将这些用于我的属性方法”非常弱。无论如何,那些属性设置者/获取者应该是私有的。将其视为一种重载形式,并将其称为SetFoo和GetFoo。

#5


In my opinion writing to global variables should be easily distinguishable from normal setters. If a global variable cannot be avoided I normally use

在我看来,写入全局变量应该很容易与普通的setter区分开来。如果无法避免全局变量,我通常会使用

SetGlobalFoo(...);

for this purpose. The overhead for the long name is OK IMO because these constructs should be rarely used.

以此目的。长名称的开销是OK IMO,因为这些构造应该很少使用。

#6


I would use SetXXX and GetXXX for private and global variables because I don´t see a difference in what those methods do. The operation to SetXXX is a set over a data area. If that data area is global, local or remote, it´s an internal detail of the method that should not be visible from outside.

我会将SetXXX和GetXXX用于私有变量和全局变量,因为我看不出这些方法的作用有什么不同。 SetXXX的操作是在数据区域上设置的。如果该数据区域是全局的,本地的或远程的,则该方法的内部细节不应从外部可见。

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将帮助您了解该数据区域是否为本地,但如果您愿意,可以编写一条简单的注释行说明。

#1


I don't want to call them "Get" or "Set" because I use those for my property methods.

我不想称它们为“Get”或“Set”,因为我将它们用于我的属性方法。

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,否则你会以一种很难的方式暴露你对象的很多内部状态控制。

#2


What Delphi version are you using? If using D2006 or 2007 you can move the globals into class properties with class methods to get and set the values. As these are property getters and setters, using Get and Set are appropriate.

您使用的是什么Delphi版本?如果使用D2006或2007,则可以使用类方法将全局变量移动到类属性中以获取和设置值。由于这些是属性getter和setter,因此使用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;

#3


Property getters and setters don't have names starting with get and set because it's some convention reserved for naming getters and setters. 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.

属性getter和setter没有以get和set开头的名称,因为它是为命名getter和setter而保留的一些约定。他们有这些名字因为这就是他们所做的。由于您的synchronized方法的目的是设置变量的值,因此给它一个“set”名称是完全合理的。

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. When you have a routine that sets the value of Foo, convention dictates that the function must be named 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.

您可以选择同义动词,例如分配或复制,只是为了与set不同,但这些是您描述的目的的非常规名称。当你有一个设置Foo值的例程时,约定规定该函数必须命名为SetFoo。最后,我认为你只需要克服你使用get和set为非属性访问器设置的任何挂断。

#4


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. Those property setter/getters should be private anyway. Consider it a form of overloading and call them SetFoo and GetFoo.

我会说Code Complete的建议相当强烈,你的反对意见“因为我将这些用于我的属性方法”非常弱。无论如何,那些属性设置者/获取者应该是私有的。将其视为一种重载形式,并将其称为SetFoo和GetFoo。

#5


In my opinion writing to global variables should be easily distinguishable from normal setters. If a global variable cannot be avoided I normally use

在我看来,写入全局变量应该很容易与普通的setter区分开来。如果无法避免全局变量,我通常会使用

SetGlobalFoo(...);

for this purpose. The overhead for the long name is OK IMO because these constructs should be rarely used.

以此目的。长名称的开销是OK IMO,因为这些构造应该很少使用。

#6


I would use SetXXX and GetXXX for private and global variables because I don´t see a difference in what those methods do. The operation to SetXXX is a set over a data area. If that data area is global, local or remote, it´s an internal detail of the method that should not be visible from outside.

我会将SetXXX和GetXXX用于私有变量和全局变量,因为我看不出这些方法的作用有什么不同。 SetXXX的操作是在数据区域上设置的。如果该数据区域是全局的,本地的或远程的,则该方法的内部细节不应从外部可见。

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将帮助您了解该数据区域是否为本地,但如果您愿意,可以编写一条简单的注释行说明。