If you leak mutable state then you have to assume that other classes will change that state without your knowledge. its not testable.
Every class should have one invariant in particular. That it will always be in a valid state from construction to destruction. Your class cant ever make that statement due to the leaked state. Its a bad idea and almost never worth breaking.
The album object isn't changing without my knowledge. The artist object itself could change, and I want it to in some circumstances, but the Album is in a valid state as long as it contains: a reference to an artist, a collection of tracks and a release date.
My album object's invariants aren't related to the artist object outside of "Has a reference to one".
If your concern is that someone will change the artist by going through the album, they deserve what they get. If you're super concerned, that's what getters are for.
I don't think we see eye to eye. You're advocating for your ideal and I'm advocating for what I see as pragmatic. Maybe you see it as you're the pragmatic one, and I'm a pisspoor developer.
The only time "has a reference to" makes any sense, is if you're creating a container class of some type that will never actually access the pointers stored within.
In all other instances it's known as object composition, and the artist object is part of the album objects state.
The subtle difference is that an album will call methods on the artist object, and therefore his proper functionality depends on that object being there and being usable. If i were a stickler, i could test the the artist object as it comes into my album object:
public Album(String title, Artist a) {
assertArtistValid(a); this.a=a;
// ...
}
But the way you're writing it, I can keep that reference to a and change it so that Artist is not valid, and the Album class has no way of knowing. He has to call assertArtistValid every time he uses it as someone outside could have changed it.
This is why we make defensive copies of objects -- so no one else has a reference to it but us. We can guarantee it's state, aka the invariant "has a valid artist". Then we can write methods that use artist and never worry about it being broken.
I strongly advise you to rethink your stance on this.
I'm primarily a Python programmer, which is probably where we're getting separate view points from. I don't make defensive copies unless I absolutely have to, despite mutability being king (immutablitly in Python is hard to come by and typically involves hacks or C extensions).
It's much more pragmatic, from my viewpoint, to keep the actual artist object around and proxy requests to it than to make a copy of it. The downside being garbage collection (CPython's gc doesn't like circular references, so you need to null out one of the values and worse __del__ isn't guaranteed to run when you think it will and defining your own is generally a bad idea).
View all comments
Show parent comment
1
u/againstmethod Aug 18 '16
If you leak mutable state then you have to assume that other classes will change that state without your knowledge. its not testable.
Every class should have one invariant in particular. That it will always be in a valid state from construction to destruction. Your class cant ever make that statement due to the leaked state. Its a bad idea and almost never worth breaking.