Public and private properties, naming conventions

So, continuing my Q&A with the user from the NLSC forums, he asked me two things. One, it was his first time that he saw someone access a class property directly, like so:

instance.property = newValue

And of course it hit me, that the proper way is for properties in classes to be private, and to provide get/set methods to the users if need be. Getting used to the automatic properties and fields of C# makes you forget about stuff like that. And that can only help emphasize my point about not learning a “modern” programming language first.

Regarding the first part, you can use instance.property = something (e.g. mycar.Make = “Volvo”) as long as you set the property to public. The “correct” programming practice says that you set all properties to private, and only give access to them IF YOU WANT to the user through methods, like so:

class Car()
{
  private string make;

  public string getMake()
  {
    return make;
  }

  public void setMake(string make)
  {
    this.make = make;
  }
}

So for example, if you don’t want the user to be able to set the make of the car, you just never create the setMake method, so the “make” property becomes read-only. The user can only get it.

So, private properties can only be accessed by methods inside the class (and so the user needs something like getMake, setMake), public properties can be accessed directly by the user (e.g. mycar.Make = “Volvo” or print mycar.Make).

Second question was whether all method parameters need to have the same name as the class properties. Short answer is no.

Regarding the second part of your question, no, you don’t need to have the same name for the class’ properties and the parameters of the constructor or any methods. So the above setMake (or any constructor, similarly) can be:

public void setMake(string nameOfCompany)
{
  make = nameOfCompany;
}

This way, you avoid using the “this” parameter. It’s up to you. Early on you may want to avoid using it, but as you get more experience, you may find that the property’s name is the logical name for the parameter as well, so you may want to use “this”. Another solution that’s really common, is that private properties (called “fields” usually) are named starting with an underscore (_), like so:

private string _make;

public void setMake(string make)
{
  _make = make;
}

This is a really nice solution, since it makes the private properties really clear that they are exactly that, private properties.

In the end, it’s up to you. Coding and naming conventions are there to make code easier for you to read again later, and for others to understand, but it’s not like any compiler will complain about the names you use. As I said, up to you.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s