Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying setter argument - is it hack or not?

Is it normal to modify setter arguments? Let's imagine that we have setString method. And we really want to keep a trimmed form of the string. So a string with trailing spaces is invalid, but we don't want to throw an exception.

What's the best solution? To trim the value in the setter e.g.

public void setString(String string) {
    this.string = string.trim();
}

Or trim it in the caller (more than once) e.g.

object.setString(string.trim());

Or maybe something else?

like image 499
shkutkov Avatar asked Dec 22 '22 13:12

shkutkov


1 Answers

Yes. After all, setters are designed for these kind of things! To control and sanitize the values written to fields ;)

like image 192
mmx Avatar answered Jan 13 '23 22:01

mmx