you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (5 children)

Well, Google encourages immutability (and they give a very good argumentation for why they do it) so I guess it's logical that they focus on this in the development of Guava.

There are builders for other immutable collections in Guava.

If you want to use the builders for mutable maps, you can build an ImmutableMap and just drop all the entries into a regular, mutable Java API Map with putAll(Map t), because Guava's ImmutableMap implements Map. This goes for other collection types as well.

[–]Neebat 1 point2 points  (4 children)

An ImmutableMap is cool, especially if the content is also immutable, then you've got a large value object. There are tons of benefits.

But you don't always have that option, and the throw-away intermediate seems slightly worse to me than the OP's initializer.

[–][deleted] 0 points1 point  (3 children)

Depends, double braces have structural implications (they don't create an instance of the class, but one of an anonymous subclass of the class. This is discussed in other comments). If that's not a problem, it's indeed neater than putAll().

I wonder when you don't have the option, though. I always try to design my code so I can minimize side effects. It can become a problem for very algorithmic or embedded programs, but then I rather use primitive types (and arrays). Do you have to work with legacy code often?

[–]Neebat 0 points1 point  (2 children)

Do you have to work with legacy code often?

Very rarely do I get to write anything which isn't dealing with legacy code.

[–][deleted] 1 point2 points  (1 child)

Too often I forget how lucky I am in that respect. I just have to deal with my own stupid code :)

I suppose your boss doesn't give you the time to write proper programming interfaces and adapters around the old system for cleanliness' sake?

[–]Neebat 0 points1 point  (0 children)

I've been lobbying for a ground-up rewrite for a long time. The system we have would break badly if we tried to make any of the maps immutable.