构造函数参数的命名约定?

发布于 2024-11-02 09:57:30 字数 838 浏览 1 评论 0原文

我总是遇到一个问题,类中的公共变量的名称与我传递给相同类构造函数的参数之间的相似性。

当您定义对象的新实例时,例如 Car。用户/程序员唯一能看到的是它正在寻找的参数的名称和类型。

例如:

public Car(Color BodyColor, int NumOfDoors, int SizeOfWheels)
{
}

用户将看到这些名称和类型,并且只能根据其类型和名称知道它们是什么,不包括任何 xml 摘要标记。

现在,我们总是希望我们的公共变量也非常具体。

例如:

public Color BodyColor { get; set; }
public int NumOfDoors { get; set; }
public int SizeOfWheels { get; set; }

现在,我们可以回答我的问题了。使用这些单行属性,而不是定义变量的私有实例并为其创建属性,如何使这些变量名称更清晰?

我正在尝试使用其他用户会发现易于理解且易于阅读的命名约定。

现在构造函数看起来像

public Car(Color BodyColor, int NumOfDoors, int SizeOfWheels)
{
     this.BodyColor = BodyColor;
     this.NumOfDoors = NumOfDoors;
     this.SizeOfWheels = SizeOfWheels;
}

其他 C# 程序员会写的那样吗?是否已经存在为此的命名约定?乍一看,上面的语句看起来有点混乱,特别是如果省略 this.

I always have an issue with similarities between the names of the Public variables in a class and the arguments I am passing into the same classes constructor.

When you are defining a new instance of an object, for example, Car. The only thing that the user/programmer can see is the names and type of the arguments that is it looking for.

For Example:

public Car(Color BodyColor, int NumOfDoors, int SizeOfWheels)
{
}

The user would see these names and types, and would only be able to know what they are based on their type and name, exluding any xml summary tags.

Now, we always want our public variables to be very specific as well.

For Example:

public Color BodyColor { get; set; }
public int NumOfDoors { get; set; }
public int SizeOfWheels { get; set; }

Now, we can get to my question. Using these one-line properties, instead of defining a private instance of the variable and creating the property for that, how does one make these variable names more clear?

I am trying to use naming conventions that other users will find understandable and easy to read.

Right now the constructor looks like

public Car(Color BodyColor, int NumOfDoors, int SizeOfWheels)
{
     this.BodyColor = BodyColor;
     this.NumOfDoors = NumOfDoors;
     this.SizeOfWheels = SizeOfWheels;
}

Is this what other C# programmers would write? Is there a naming convention that already exists for this? At first glance, the above statement looks a tad messy, especially if you omit the this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

似最初 2024-11-09 09:57:30

我从未见过第一个字母大写的方法参数。我建议不要这样做,因为它不是标准。 “这个”也变得不必要了。在我看来,下面的代码更具可读性。此外,您还会注意到 .NET API 调用的方法参数的首字母不是大写的。这适用于任何函数原型,而不仅仅是构造函数。

编辑:如果您的属性仅在构造函数中设置,我建议将设置器设为私有(此处的示例代码中未显示)。如果不再设置这些值,另一个好的做法是让它们由字段支持,并将该字段设置为只读。这有点超出了您的问题范围,但与定义和命名字段和属性有关。

public Car(Color bodyColor, int numOfDoors, int sizeOfWheels)
{
     BodyColor = bodyColor;
     NumOfDoors = numOfDoors;
     SizeOfWheels = sizeOfWheels;
}

注意:与原始问题中的代码相比,即使堆栈溢出语法突出显示也使其具有可读性。

I have never seen method parameters with the first letter capitalized. I would suggest not doing this as it is not a standard. The 'this' also becomes unnecessary. The below code is more readable, in my opinion. Also, you will notice that .NET API calls' method parameters do not have their first letter capitalized. This applies to any function prototype, not just constructors.

Edit : If your properties are only SET in the constructor, I would suggest making the setters private (not shown in my example code here). Another good practice, if the values are never set again, is to have them backed by a field, and make the field read-only. That is a bit out of scope of your question, but is related to defining and naming fields and properties.

public Car(Color bodyColor, int numOfDoors, int sizeOfWheels)
{
     BodyColor = bodyColor;
     NumOfDoors = numOfDoors;
     SizeOfWheels = sizeOfWheels;
}

Note: Even the Stack Overflow syntax highlighting makes it readable compared to the code in your original question.

情仇皆在手 2024-11-09 09:57:30

.net 约定是对函数参数使用驼峰式大小写。因此,您的构造函数可能如下所示:

public Car(Color bodyColor, int numOfDoors, int sizeOfWheels)
{
}

另外,就属性而言,您的大小写是正确的,但通常您可以为属性设置私有实例字段,而不是直接设置属性本身 - 假设您不这样做如果该属性在设置端没有任何逻辑。

我通常使实例字段以下划线开头,因此从构造函数中设置属性的实例字段可能看起来更像以下内容:

public Car(Color bodyColor, int numOfDoors, int sizeOfWheels)
{
     this._bodyColor = bodyColor;
     this._numOfDoors = numOfDoors;
     this._sizeOfWheels = sizeOfWheels;
}

另请阅读 MSDN .NET 框架样式设计指南,以进一步详细讨论其他样式元素。这些标准有很好的记录 - 所以实际上不应该有太多的猜测工作。 http://msdn.microsoft.com/en-us/library/ms229042.aspx 享受吧

The .net convention is to use camel casing for function arguments. So your constructor might look something like the following:

public Car(Color bodyColor, int numOfDoors, int sizeOfWheels)
{
}

Also as far as the properties go, you have the casing correct for those, but typically you might set the private instance fields for the properties instead of the properties themselves directly - that assumes that you don't have any logic in the set side if the property.

I usually make my instance fields start with an underscore so setting the instance field for the property from your constructor might look more like the following:

public Car(Color bodyColor, int numOfDoors, int sizeOfWheels)
{
     this._bodyColor = bodyColor;
     this._numOfDoors = numOfDoors;
     this._sizeOfWheels = sizeOfWheels;
}

Also please read the MSDN Design guidelines for style for the .NET framework for a further detailed discussion of other style elements. The standards are very well documented - so there really shouldn't be much guess work. http://msdn.microsoft.com/en-us/library/ms229042.aspx

Enjoy!

一袭白衣梦中忆 2024-11-09 09:57:30

请参阅此处的 MS 命名约定,以及此处

See MS naming conventions here, and here.

〆一缕阳光ご 2024-11-09 09:57:30

我也使用驼峰式大小写,但更喜欢在参数名称左侧添加“a”前缀
(“a”代表argument,它并不意味着是一篇文章)。
我对例程本地的任何变量使用驼峰式大小写,没有任何前缀。
这样做,您一眼就能看出局部变量和参数。
不过,我知道这并不常见。
我对字段使用与 Doug 相同的约定,因此您最终会得到:

public Car(Color aBodyColor, int aNumOfDoors, int aSizeOfWheels)
{
     this._bodyColor = aBodyColor;
     this._numOfDoors = aNumOfDoors;
     this._sizeOfWheels = aSizeOfWheels;
}

Just my 2 cents 。

I use camel casing as well, but prefer to add an 'a' prefix on the left of parameter name
( the 'a' stands for argument, it is not meant as an article ) .
I use camel casing without any prefix for any variable local to the routine .
Doing so, you can tell at first glance a local variable from a parameter .
I understand that this is not common practice, though .
I use the same conventions as Doug for fields, so you'll end up with :

public Car(Color aBodyColor, int aNumOfDoors, int aSizeOfWheels)
{
     this._bodyColor = aBodyColor;
     this._numOfDoors = aNumOfDoors;
     this._sizeOfWheels = aSizeOfWheels;
}

Just my two cents .

烟酒忠诚 2024-11-09 09:57:30

名称绝对不应该是大写。在我看来,非常需要后缀来区分输入世界和类属性。

我不认为这有什么不好:

public Car(Color bodyColorInput, int numOfDoorsInput, int sizeOfWheelsInput)
{
     this.BodyColor = bodyColorInput;
     this.NumOfDoors = numOfDoorsInput;
     this.SizeOfWheels = sizeOfWheelsInput;
}

Names should definetively never be upper case. A suffix is, on my opinion, very needed to differentiate between those the input world and the class properties.

I don't see anything bad about this:

public Car(Color bodyColorInput, int numOfDoorsInput, int sizeOfWheelsInput)
{
     this.BodyColor = bodyColorInput;
     this.NumOfDoors = numOfDoorsInput;
     this.SizeOfWheels = sizeOfWheelsInput;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文