Hi,
Recently I'm encountering this type of code ever more frequently:
List<Something> list = new LinkedList<Something>();
// add a bunch of constants to the list
// keep in mind that the number of items added is known on list creation
return list.toArray(new Something[list.size()]);
Now in my honest opinion this is improper use of the Collections API and while I wouldn't make an issue out of this if I see it once or twice, I'm now encountering this everywhere.
I tried speaking to the author using something like: "hey why don't you use an ArrayList instead, since this is exactly what you need, because its toArray operation is constant speed". However in the authors opinion this is premature optimization and I should not care about it.
I completely agree that optimization should be done only if needed (and even than rarely :)), but in my opinion picking the correct collection to use is not premature optimization (nor is it time consuming or hard ... using LinkedList
everywhere is simply 'bad code' IMHO).
So my question is. Is me worrying about these code blocks premature optimization in your opinion ? And if not how would you approach the situation, would you speak to the author again and what arguments would you use ?