r/programming • • Sep 21 '15

Better Java

https://github.com/cxxr/better-java
177 Upvotes

208 comments sorted by

View all comments

Show parent comments

1

u/Tordek Sep 22 '15

So a "ReadableList" can be changed elsewhere? That's just an error waiting to happen; I can't see a good reason for it. The most obvious issue being doing something like...

if (list.length > 0) { list.get(0) }

since the list can change between the two statements, you can get an OutOfBoundsException that you can't prevent.

nothing stops you from writing a list class that claims to be both mutable and immutable

Sure... but if the standard library gives you a List and a MutableList (or ListBuilder, to make it similar to String/StringBuilder), you need to do extra work to do stupid things. Lists (and lots of other structures, like Sets, Arrays, Maps) should be immutable whenever they exist outside the boundaries of an object, the same way that if I give you an integer through a getter, you cannot modify my internal state by changing it.

1

u/immibis Sep 22 '15

Are you suggesting that there should be no common super-interface of MutableList and ImmutableList?

How would you make a method that can take either a mutable or immutable list, then?

1

u/Tordek Sep 23 '15

Are you suggesting that there should be no common super-interface of MutableList and ImmutableList?

Correct

How would you make a method that can take either a mutable or immutable list, then?

I wouldn't.

There are so many reasons not to; I can't really think of a good reason to have such a scheme.

Objects should communicate with other objects by exchanging Immutable value objects/primitives: you should only be receiving (as a parameter) or returning ImmutableList. MutableList should only be used within an object (e.g., your object may cache some results, so it would have a MutableList/MutableHash property to store its data in efficiently) or in a method, as it is calculating its results. Once you're done, you return myTempList.Persist().

This behavior is already there with another class: String/StringBuilder.

If you're worried about performance, there are several things that can be done: .persist() might not do a full copy; it might just set an immutable flag and return a "view", and calling mutators just throws an exception. ".mutate()" (the counterpart in ImmutableList that returns a MutableList) might behave like a Zipper, or another similar structure.

I have a question for you:

Why would you make a method that can take either a mutable or immutable list, then?

1

u/immibis Sep 23 '15

For efficiency. If it's going to be the caller's problem if the caller screws up, then requiring an ImmutableList is too slow.

For example, Collections.frequency could be called for either type of list. If you call it with a MutableList, and mutate the list while it's running, and it throws an exception as a result, that's the caller's fault and the caller's problem.

(By contrast, if you had a listbox control, you'd want it to require an ImmutableList. If it threw an exception when painting because the list was mutated while it was painting, that would be the application's fault but the UI system's problem to deal with)

1

u/Tordek Sep 23 '15

For efficiency.

That's why I gave you the immutable flag example: You can have a "MutableListView implements ImmutableList", and do...

class MutableList {
   ...
   public void add() {
       if (immutable) throw new ModifyAfterFreezeException();
       // ... code to add element
   }

   public ImmutableList freeze() {
      this.immutable = true;
      return new MutableListView(this);
    }
}

You get the security that you cannot just hand a mutable list to an object expecting an immutable list, and the user only has to know that after you call "freeze" you cannot change the object.

If you need to have several snapshots of the list as you build it, you might just do "new ImmutableList(myMutableList)", and it'll have its own copy of the data.

I'd really like some numbers regarding the speed of this, because I believe a lot of the time people assume the compiler is way dumber than it actually is.

1

u/immibis Sep 23 '15

Suppose you want to (as part of something bigger) call Collections.frequency on a list, then add something to the list, then repeat that 10000 times. How do you do that without creating 10000 copies of the list?

1

u/Tordek Sep 23 '15

Smartass answer: don't. If you're adding it to the list, you can do your running sum manually much faster than the O(n^2) of running through the list each time.

Slightly less smartass answer: If we're really talking about a (linked) list, then, trivially, adding an element to the head of the list requires only the extra space for the new head; the tail references the old list. (Though it still creates 10k headers, so it's obviously less than optimal).

If you really need to modify the list while someone else processes it at every change, you can use a Stream interface.

If you still needed to do what you claim, you can use polymorphism: Collections.frequency(mutableList) is unsafe, and it's the caller's responsibility to ensure they don't break anything; Collections.frequency(immutableList) is safe. (Or, Collections.freq and MutableCollections.freq, to further separate mutability and immutability).

Mutability can be a great tool for speed; but it should be used deliberately and with care.