Should setters silently sanitize input — or should they just throw? [closed]

Suppose the code's inside some FullName class And suppose I want only capitalized name elements (let's ignore the fact that in some cultures, name elements may start with a lower-case letter) Should I throw here if the method's client passes a lower-case last name? Or should I silently capitalize it myself? Which is a better design generally? public void setLastName(String lastName) { this.lastName = lastName; }

Jan 30, 2025 - 22:53
 0
Should setters silently sanitize input — or should they just throw? [closed]

Suppose the code's inside some FullName class

And suppose I want only capitalized name elements (let's ignore the fact that in some cultures, name elements may start with a lower-case letter)

Should I throw here if the method's client passes a lower-case last name?

Or should I silently capitalize it myself?

Which is a better design generally?

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }