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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With