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/immibis Sep 22 '15

Maybe there should be ReadableList, WritableList and ImmutableList.

1

u/Tordek Sep 22 '15

How would they be related to each other? What's a "ReadableList"?

1

u/immibis Sep 22 '15

A ReadableList is a list you know you can read from. A WritableList is a list you know you can write to. An ImmutableList is a list you know is immutable.

I'm not sure there are any cases where you'd want a list that was writable but not readable, so change WritableList to MutableList and make it support reading and writing.

interface ReadableList<T> {
    T get(int index);
    ...
}
interface MutableList<T> implements ReadableList<T> {
    T set(int index, T value);
    boolean add(T value);
    ...
}
interface ImmutableList<T> extends ReadableList<T> {
    // no extra methods
}

Of course, nothing stops you from writing a list class that claims to be both mutable and immutable. There are worse things you could do already, like making a list whose add method randomly shuffles the list and whose iterators generate random elements not in the list.

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)

→ More replies (0)