Getters/Setters. Evil. Period.

Aren't getters and setters just Java's version of C#'s property system? I thought the whole idea was if you want to add validation, logging, tracking, etc. to a property of an object later, you can just modify the set method that's already used instead of having to change all the code that calls set. Similarly for get, if you wanted to change "Weight" from being set to being calculated based off other properties you could do it without anyone outside knowing or caring.

For immutable objects approach, how would you do something like this (I'm genuinely open to an alternative, not sure how you would do it): Lets say you have a Dog editor where a user can make a picture of a dog by selecting breed, color, name, etc.. I would probably have a Dog class that had setters/getters for all the properties that default to something nice in the constructor (or from a file that was remembered from a previous session) but can be changed. When a new color is selected in the editor, you would do something like this:

public class DogEditor extends SomeMoreGenericEditor {
    private Dog mDog = new Dog();
    private DogRenderer mRenderer = new DogRenderer();
//...
    protected void onColorChanged(Color newColor) {
        mDog.setColor(newColor);
    }
//...
    protected void onDraw(Canvas canvas) {
        mRenderer.render(mDog, canvas);    
    }
//...
}

The dog could be drawn with a DogRenderer, which would use getters on the Dog (which extends some more generic Renderer):

public class DogRenderer extends Renderer {
//...
    public void render(Dog dog, Canvas canvas) {
        canvas.fillShape(dog.getColor(), dog.getShape());
        // Draw other things based off the dog's properties here
        // (Could also call them dog.color() and dog.shape(), but they would do the same thing)
    }
//...
}

I could see you instead of having a DogRenderer and Dog, be a more generic AnimalRenderer and Animal, with an AnimalEditor. In any case it seems like getters and setters work well here rather than constructing a new Dog whenever something about the Dog changes.

tl;dr; I think gets/sets have a place in the absence of properties.

/r/programming Thread Link - yegor256.com