构造函数参数的命名约定?
我总是遇到一个问题,类中的公共变量的名称与我传递给相同类构造函数的参数之间的相似性。
当您定义对象的新实例时,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我从未见过第一个字母大写的方法参数。我建议不要这样做,因为它不是标准。 “这个”也变得不必要了。在我看来,下面的代码更具可读性。此外,您还会注意到 .NET API 调用的方法参数的首字母不是大写的。这适用于任何函数原型,而不仅仅是构造函数。
编辑:如果您的属性仅在构造函数中设置,我建议将设置器设为私有(此处的示例代码中未显示)。如果不再设置这些值,另一个好的做法是让它们由字段支持,并将该字段设置为只读。这有点超出了您的问题范围,但与定义和命名字段和属性有关。
注意:与原始问题中的代码相比,即使堆栈溢出语法突出显示也使其具有可读性。
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.
Note: Even the Stack Overflow syntax highlighting makes it readable compared to the code in your original question.
.net 约定是对函数参数使用驼峰式大小写。因此,您的构造函数可能如下所示:
另外,就属性而言,您的大小写是正确的,但通常您可以为属性设置私有实例字段,而不是直接设置属性本身 - 假设您不这样做如果该属性在设置端没有任何逻辑。
我通常使实例字段以下划线开头,因此从构造函数中设置属性的实例字段可能看起来更像以下内容:
另请阅读 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:
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:
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!
请参阅此处的 MS 命名约定,以及此处。
See MS naming conventions here, and here.
我也使用驼峰式大小写,但更喜欢在参数名称左侧添加“a”前缀
(“a”代表argument,它并不意味着是一篇文章)。
我对例程本地的任何变量使用驼峰式大小写,没有任何前缀。
这样做,您一眼就能看出局部变量和参数。
不过,我知道这并不常见。
我对字段使用与 Doug 相同的约定,因此您最终会得到:
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 :
Just my two cents .
名称绝对不应该是大写。在我看来,非常需要后缀来区分输入世界和类属性。
我不认为这有什么不好:
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: