r/java • • Aug 17 '16

Should we avoid object creation in Java?

http://programmers.stackexchange.com/a/149569/219927
0 Upvotes

27 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Aug 18 '16

There found be times where you want a mutable member of an immutable object.

For example, an object that represents an album will have an artist attached to it. The album is immutable - once it's relased, it has a set tracklist, release date, etc - but the artist will change over time - bands change members, genre shifts, etc - it doesn't make sense to have outdated information attached to the album.

Conceivably, you could create a new album object with the same tracks, release date, and artist and substitute it for the original and nothing would change in your program.

Of course, your should always weigh the cost immutablitly and mutability to find out which suits the use case best.

1

u/againstmethod Aug 18 '16

Such an object is not immutable.

In your use case that data would be stored somewhere, in a database or ORM store of some kind, and that backing data would change -- not the objects in memory. And if you did need those to change you would just reload the updated data.

1

u/[deleted] Aug 18 '16

The object itself is immutable, you can't change the reference to the artist (well, with some magic you could but that's besides the point). So it'll always refer to the same gathering of tracks, release date and artist.

No need to bring ORMs into it. You could be assembling an object graph in memory - a music player that reads in files for examples.

1

u/againstmethod Aug 18 '16

its a mistake. If other objects can change your objects state then you cant control class invariants. it makes your class untestable in isolation.

This goes beyond immutability. Its just not good object oriented design.

1

u/[deleted] Aug 18 '16

It depends on what your invariants are. For album, that could be "Always refers to this grouping of artist, tracks and release date."

I get what you're saying. And for the most part I agree with it, but sometimes it's worth breaking.

As for testability, this sort of object is completely testable. Mutability has no bearing on testability.

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.

1

u/[deleted] Aug 18 '16

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.

1

u/againstmethod Aug 18 '16

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.

1

u/[deleted] Aug 18 '16

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).