简体   繁体   English

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

[英]Naming conventions for Constructor Arguments?

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.当您定义对象的新实例时,例如 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.用户将看到这些名称和类型,并且只能根据它们的类型和名称知道它们是什么,排除任何 xml 摘要标签。

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?这是其他 C# 程序员会写的吗? 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.乍一看,上面的语句看起来有点乱,特别是如果你省略了this.

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.此外,您会注意到 .NET API 调用的方法参数的首字母没有大写。 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.注意:与原始问题中的代码相比,即使是 Stack Overflow 语法突出显示也使其具有可读性。

I use camel casing as well, but prefer to add an 'a' prefix on the left of parameter name我也使用驼峰式大小写,但更喜欢在参数名称的左侧添加一个“a”前缀
( the ' a ' stands for argument , it is not meant as an article ) . (' a ' 代表论点,它不是文章)。
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 :对于字段,我使用与 Doug 相同的约定,因此您最终会得到:

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

Just my two cents .只是我的两分钱。

The .net convention is to use camel casing for function arguments. .net 约定是对函数参数使用驼峰式大小写。 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.另请阅读 MSDN Design Guidelines for style for the .NET framework 以进一步详细讨论其他样式元素。 The standards are very well documented - so there really shouldn't be much guess work.这些标准有很好的文档记录 - 所以真的不应该有太多的猜测工作。 http://msdn.microsoft.com/en-us/library/ms229042.aspx http://msdn.microsoft.com/en-us/library/ms229042.aspx

Enjoy!享受!

请参阅此处此处的MS 命名约定。

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;
}

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

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