0

Should getters have validation in java(for example. checking if something is null)? or should they simply get whatever it is suppose to get with one return statement. To me this is how i usually do it.

public class GetterDemo
{
private String name;
private int age;
public GetterDemo(String name, int age)
{
     this.name = name;
     this.age = age;
}

public String getName()
{
return this.name;
}

public int getAge()
{
return this.age;
}
}

Now suppose that the constructor has an array of hobbies from a class called hobbies.

public class Hobbies
{
private String hobby;
private String category;
public Hobbies(String hobby, String category)
{
this.hobby = hobby;
this.category = category;
}

public String getHobby()
{
return this.hobby;
}

public String getCategory()
{
return this.category;
}
}

Now, lets update version of GetterDemo with an array of hobbies in the constructor as i said above. and it has a next method to get the next hobby in the array everytime its called.

public class GetterDemo
{
private String name;
private int age;
private int count = 0;
private Hobbies[] hobbies;
public GetterDemo(String name, int age, Hobbies[] hobbies)
{
this.name = name;
this.age = age;
this.hobbies = hobbies
}

public Hobbies getNextHobby()
{
//Create a counter.
Hobbies result;
if (this.hobbies == null || this.hobbies.length == 0
  || this.count >= this.hobbies.length) //Called more then length times.
return null;

result = this.hobbies[count];
count++;
return result;  
}

public String getName()
{
return this.name;
}

public int getAge()
{
return this.age;
}
public void reset()
{
this.count = 0;
}
}

Okay so thats a little code example. There might be errors, probably many as I blankly coded it. To explain in terms of testing for null(JUnit testing). If I call getNextHobby() hobby length times or more it will return null and I can AssertNull() and it will pass. However, for example, if I do AssertNull where the array of hobbies is Null and I try getNextHobby.getCategory(), it will throw a NullPointerException even though I want it to be null. Would the only way to fix this would be to create a method that checks for this? or a getter that checks for null somehow? Possibly the code below?

public boolean checkNull()
{
boolean result = false;
if (getNextHobby().getCategory() == null || getNextHobby().getHobby())
result = true;
return result;
}
Nick DeFazio
  • 2,412
  • 27
  • 31
CabDude
  • 132
  • 2
  • 11

1 Answers1

2

I assume that it is a matter of taste.

I prefer that setters and getters do only what they suppose to do which means set some value or get some value. My reasons are the following:

  1. The signature of the method should explain what this method is doing and as we know good method should do one thing well. Thus when you start adding some validation I would assume that you need to change the signature otherwise the caller might assume that only set/get operation happens.
  2. Every IDE has an opportunity to generate getters and setters and for some reason they don't generate some validations for the parameters of the getter method thus I assume that you shouldn't do it as well.
  3. I try to separate getters and setters from other methods, thus if I need to kind of get the value, however, it is not exactly a getter because you need to do some other actions, I prefer to use other words like fetch, retrieve, obtain. In this case it will be very clear that you don't need to test getters and setters because they don't really do anything, however, fetch and others you must.
Rufi
  • 2,529
  • 1
  • 20
  • 41