Should getters have validation in java?

CabDude

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Should Java POJO have field validation and throw exceptions in setter methods?

From Java

Should Java 8 getters return optional type?

From Dev

Should Java interface only contain getters?

From Dev

Should Foreign Keys have corresponding getters and setters in Room Persistence Library?

From Dev

MVC - validation - should ViewModel have access to DB

From Dev

Rails STI validation should have one

From Dev

MVC - validation - should ViewModel have access to DB

From Dev

In Java, should I use getters or interface tagging for constant properties?

From Dev

Getters and setters not working as it should

From Dev

Is it conventional for Java Bean Classes to have more functionality than just getters/setters?

From Dev

Java Final Getters

From Dev

Java: Getters, Setters, and Constructor

From Dev

Access safety of getters in Java

From Dev

Getters and Setters for ArrayLists in Java

From Dev

Setters and getters with composition in Java

From Dev

Java getters and setters not working

From Dev

Getters, Setters , Object Java

From Dev

Java Final Getters

From Dev

Bean Validation List<?> on static method or getters

From Dev

Symfony Form Validation getters always triggering/failing

From Dev

Should I use noexcept for getters always?

From Dev

Should I use getters and setters in constructors?

From Java

Error: Schema validation failed with the following errors: Data path "" should NOT have additional properties(project)

From Dev

Error: The tuning parameter grid should have columns fL, usekernel, adjust. K fold Cross Validation

From Dev

Why does this Java code have this age validation date comparison?

From Dev

Can I have just getters and not setter?

From Dev

Can a JavaBean have methods besides getters and setters?

From Dev

Can Unit or Integretation Test have getters and setters?

From Dev

Is it possible to have getters and setters in typescript declaration file?

Related Related

  1. 1

    Should Java POJO have field validation and throw exceptions in setter methods?

  2. 2

    Should Java 8 getters return optional type?

  3. 3

    Should Java interface only contain getters?

  4. 4

    Should Foreign Keys have corresponding getters and setters in Room Persistence Library?

  5. 5

    MVC - validation - should ViewModel have access to DB

  6. 6

    Rails STI validation should have one

  7. 7

    MVC - validation - should ViewModel have access to DB

  8. 8

    In Java, should I use getters or interface tagging for constant properties?

  9. 9

    Getters and setters not working as it should

  10. 10

    Is it conventional for Java Bean Classes to have more functionality than just getters/setters?

  11. 11

    Java Final Getters

  12. 12

    Java: Getters, Setters, and Constructor

  13. 13

    Access safety of getters in Java

  14. 14

    Getters and Setters for ArrayLists in Java

  15. 15

    Setters and getters with composition in Java

  16. 16

    Java getters and setters not working

  17. 17

    Getters, Setters , Object Java

  18. 18

    Java Final Getters

  19. 19

    Bean Validation List<?> on static method or getters

  20. 20

    Symfony Form Validation getters always triggering/failing

  21. 21

    Should I use noexcept for getters always?

  22. 22

    Should I use getters and setters in constructors?

  23. 23

    Error: Schema validation failed with the following errors: Data path "" should NOT have additional properties(project)

  24. 24

    Error: The tuning parameter grid should have columns fL, usekernel, adjust. K fold Cross Validation

  25. 25

    Why does this Java code have this age validation date comparison?

  26. 26

    Can I have just getters and not setter?

  27. 27

    Can a JavaBean have methods besides getters and setters?

  28. 28

    Can Unit or Integretation Test have getters and setters?

  29. 29

    Is it possible to have getters and setters in typescript declaration file?

HotTag

Archive