My only reason for using arrays would be for conciseness as per Rob's point 1)
but as Dan says the List equivalent can be as good if we use Arrays.asList(),
so:
+1 @ "replace arrays with lists"
Mike
On 20 Dec 2010, at 08:35, Dan Haywood wrote:
> Hi Rob,
>
>
> On 19/12/2010 21:18, Robert Matthews wrote:
>> Should we replacing all arrays with lists?
> Yes, I think so. Lots of reasons:
> - lists can be made immutable, which will reduce defects (in fact, I'd like
> us to get to evolve the metamodel APIs so that they are fully immutable)
> - lists correctly support covariance/contravariance, whereas arrays do not
> (ArrayStoreException anyone).
> - list lets us change the implementation (ArrayList vs LinkedList or some
> other mechanism) whereas an array exposes its "implementation"
> - lists can be decorated, eg made thread-safe (Collections.synchronized*())
> - lists are a higher level of abstraction compared to arrays
>
> There's also pragmatic reasons:
> - with google-collections there is great support for processing lists in a
> declarative way (eg apply a Function to transform).
> - just compare appending one list to another (list.addAll(otherList) vs the
> huge amount of code needed to do the same thing with arrays)
> - lists can grow in size, which simplifies the code
>
> Others think so too:
> - Josh Bloch, in Effective Java (item 25); see [1]
> - javapractices.com [2]
> - these responses on stackoverflow [3]
>
> To answer your points:
>>
>> 1) A simple statement like
>>
>> ObjectAction action = spec.getObjectAction(ObjectActionType.USER,
>> "userFor", new ObjectSpecification[] {userid.getSpecification()});
>>
>> becomes
>>
>> List<ObjectSpecification> parameters = new
>> ArrayList<ObjectSpecification>();
>> parameters.add(spec);
>> ObjectAction action =
>> userAdminService.getSpecification().getObjectAction(ObjectActionType.USER,
>> "userFor", parameters);
>>
>> Which is not much of an improvement!
>>
> or just:
> ObjectAction action = spec.getObjectAction(ObjectActionType.USER,
> "userFor", Arrays.asList(userid.getSpecification()));
>
> which is actually shorted than the array equivalent!
>
>
>> 2) If that was the right thing to do then java.lang.reflect.Method would
>> have the following method
>>
>> Object invoke(Object object, List<Object> parameters)
>>
>> to replace
>>
>> Object invoke(Object object, Object[] parameters)
>>
>> but it doesn't. My Java in a nutshell says it has this instead
>>
>> Object invoke(Object object, Object... parameters)
>>
>> which is an array by another name.
>>
> Well, there are lots of APIs from Java 1.0 that aren't the best
> (java.util.Date, anyone?).
>
> In general the java.lang.reflect does use lots of arrays, you are right, (eg
> Class#getDeclaredMethods() returning Method[]), but I think that's because
> they wanted typesafety and generics didn't exist.
>
>
>> 3) The Java people haven't suggested removing arrays from use so they must
>> be good for something.
>>
> Well, they've never removed anything....
>
> It's fine as an underlying data structure (after all, ArrayList is backed by
> an array), but it isn't appropriate for API.
>
>
>> Maybe (2) is a clue as to how we should be handling these situations where
>> all want to do is pass in a set of somthing.
>>
>> Regards
>> Rob
>>
>
> I think the original reason you probably because of their typesafety
> (covariance issues aside). However, generics mean that isn't a factor
> anymore.
>
> In general, I want the API for Isis to evolve and keep pace with recognized
> best practice, eg being immutable wherever possible. I truly think that
> doing this will help us build community; so that a casual would-be user will
> browse our API and "grok" it immediately. This is just one step along that
> journey.
>
> I'd be interested in other opinions here; we have lots of experienced Java
> guys here. Mark, Sigi, Nour, Kevin, Dave, Alexander, Mike, Sabine, Vango,
> anyone else listening in ... what say you all?
>
> Cheers
> Dan
>
> [1]
> http://books.google.co.uk/books?id=ka2VUBqHiWkC&pg=PA119&lpg=PA119&dq=java+prefer+lists+over+arrays&source=bl&ots=yXLhQhqZRZ&sig=bCY4OyUfzvgE17-l2HEHbi5cGyA&hl=en&ei=HBEPTfCtDJO6hAf274i3Dg&sa=X&oi=book_result&ct=result&resnum=7&sqi=2&ved=0CEYQ6AEwBg#v=onepage&q&f=false
> [2] http://www.javapractices.com/topic/TopicAction.do?Id=39
> [3]
> http://stackoverflow.com/questions/2391553/why-is-it-preferred-to-use-lists-instead-of-arrays-in-java
> [4]
> http://stackoverflow.com/questions/1589813/when-to-use-a-list-over-an-array-in-java
>