Getter, Setter, Worse or Better?
Forgive the technical diversion, but this is a little unsettling. I’ve been stubbornly clinging to the idea that I can get away with using integrated getter/setter methods in my code. I thought it was an elegant solution to the clumsiness of making separate methods for getting and setting values in my objects: simply check to see if there is a value passed to the method, massage the value any way I want, then return it. If no value is sent, the value gets returned without being set. One method, two uses. That’s a two birds with one stone solution, right?
Maybe not. I’ve stumbled on a situation where it will be useful to return an error message any time someone sets the value incorrectly, including an attempt to set the value to a blank string. Using a combined getter/setter method, I’ll need to return an array of values, starting with the (possibly newly set) value and adding the error description to the mix. Less elegant to check the output of the second element of an array to see whether the value was set correctly.
Folks told me I should make separate getters and setters and I didn’t listen. This could mean a lot of code to rewrite.