Coding Properties… “The Right Way”

|

In object oriented programming there is the concept of classes and/or objects. These are nice encapsulated bits of code that allow one to store data and/or functionality. One of the ways to add data to an object is to give it some properties. The “right way” of doing this usually involves adding some private data members and writing methods to a) retrieve data stored in the data members and to b) put data into the data members or set their value. These methods are usually classified as getters and setters or accessors and mutators. Why wouldn’t one just create public data members that can be set or retrieved programmatically without going through a proxy method? Well, the argument is in a word… control. Perhaps there needs to be some data validation before setting the value on a member. Perhaps one needs to modify the value of a member before returning it. These are both valid programming concerns. But what if there does not need to be any of these validations or modifications?

The Right Way

This is an example of a simple Java class that contains four data members with getter and setter methods as one might be taught in an institution of higher learning or even from one’s peers.

public class MyClass
{
  private String one;
  private String two;
  private String three;
  private String four;

  public String getOne()
  {
    return one;
  }
  public String getTwo()
  {
    return two;
  }
  public String getThree()
  {
    return three;
  }
  public String getFour()
  {
    return four;
  }

  public void setOne(String value)
  {
    one = value;
  }
  public void setTwo(String value)
  {
    two = value;
  }
  public void setThree(String value)
  {
    three = value;
  }
  public void setFour(String value)
  {
    four = value;
  }
}

Listing 1

As you can see I have created four String data members. Each data member has a get method which simply returns it’s value and a set method which simply sets it’s value. This is extremely basic and straightforward.

The Wrong Way

Now this is the same class done “the wrong way”. You would never ever code a class like this if one of your peers or teacher were looking over your shoulder, would you?

public class MyClass
{
  public String one;
  public String two;
  public String three;
  public String four;
}

Listing 2

This class is functionally equivalent to the first class. It has the same four properties who’s values you can set and retrieve.

Questions

So why is the second implementation so wrong? Does it not achieve the same end? I would further put forth the question, “which implementation is easier to maintain?” Which implementation has fewer lines of code? The way I have coded it here, to add another property would require nine lines of code in the first implementation but only one line in the second. If you did not care about formatting as much you could get away with three lines for the first implementation, but that is still two lines more than the second implementation. What if you were creating a class with twenty properties?

So what is the right way? I do not believe it is any one way at all. Use your brain! It can change between circumstances. You have to consider many different factors such as maintainability, readability, complexity, efficiency, functionality, man-hours required (yes, we have to consider business needs too), etc. As a software engineer we have to be flexible and ready to adapt to each and every new situation that arises. If you don’t… you will stagnate and become obsolete.

Do you have any thoughts on the right way to code properties? How do you do it? Please leave any feedback in the comments section and thank you for visiting my blog!