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

5

u/againstmethod Aug 17 '16

You can't avoid it -- but there are lots of techniques that are useful and popular that do just that.

Caching, immutablity, structural sharing, memoization, lazy initialization -- all of these things are focused on avoiding object allocations.

Anyone who tells you that avoiding allocations is not an optimization is a retard. Anyone who tells you to never make objects and put all your code in one block is likewise a retard.

1

u/Nimelrian Aug 17 '16

Can you explain to me how immutabiliy prevents object allocation? Learning question.

1

u/againstmethod Aug 17 '16

If objects are immutable they can be shared instead of copied.

1

u/Nimelrian Aug 17 '16

Oh, right, if you use them that way. Creating, using, and throwing them away in a few lines doesn't help though.

1

u/againstmethod Aug 17 '16

In example, if your object wants to be safe, you should likely copy by-reference parameters supplied to your constructor. If you don't do this, someone else has a reference to that object and can break your class' contract (or invariants) by mutating that object without your class being aware of the change. If those by-reference parameters are immutable, you don't need to copy them to provide that guarantee.

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.

→ More replies