Java best practices, add to collection before or after object has been modified?

To be on the safe side, you should modify before adding, unless there is a specific reason you cannot do this, and you know the collection can handle the modification. The example can reasonably be assumed to be safe, since the general List contract does not depend upon object attributes - but that says nothing about specific implementations, which may have additional behavior that depends upon the object's value.

To be on the safe side, you should modify before adding, unless there is a specific reason you cannot do this, and you know the collection can handle the modification. The example can reasonably be assumed to be safe, since the general List contract does not depend upon object attributes - but that says nothing about specific implementations, which may have additional behavior that depends upon the object's value. TreeSet, and Maps in general do no tolerate modifying objects after they have been inserted, because the structure of the collection is dependent upon the attributes of the object.

For trees, any attributes used by the comparator cannot be changed once the item has been added. For maps, it's the hashCode that must remain constant. So, in general, modify first, and then add.

This becomes even more important with concurrent collections, since adding first can lead to other collection users seeing an object before it been assigned it's final state.

– David Young Jul 26 '10 at 21:59 Yes, I mean that. Sorry, I have a cold. :) – mdma Jul 26 '10 at 22:01 Ya no reason not to always modify the object first.

Even though sometimes you can get away with modify after, it is harder to read and no less code than just doing it up front. – bwawok Jul 26 '10 at 22:14 1 @bwawok: And what if you add a persistent object who's id will be set later, when inserted into the db? Here is a reason (and this is also why you should not use the primary key in the equals/hashCode implementation).

– Pascal Thivent Jul 26 '10 at 22:18 @ Pascal Thivent If you are at the point of adding objects to a list, you would either already have the primary key back form the database, or you would be doing a loop and update of a list that was just inserted. Again, can't think of a case where you would need to do it this way. – bwawok Jul 26 '107 at 0:17.

The example you provided won't have any issues because you're using a List collection which doesn't care about the Object contents. If you were using something like TreeMap which internally sorts the contents of the Object keys it stores it could cause the Collection to get into an unexpected state. Again this depends on if the equals method uses the attribute you're changing to compare.

The safest way is to modify the object before placing it into the collection.

One of the good design rules to follow, is not to expose half-constructed object to a 3rd party subsystem. So, according to this rule, initialize your object to the best of your abilities and then add it to the list. If objects is an ArrayList then the net result is probably the same, however imaging if objects is a special flavor of List that fires some kind of notification event every time a new object is added to it, then the order will matter greatly.

In my opinion its depend of the settted attribure and tyle of collection, if the collection is a Set and the attribute have infulance on the method equal or hascode then definitely I will set this property before this refer also to sorterd list etc. In other cases this is irrelevant. But for this exapmle where object is created I will first set the atributes than add to collection because the code is better organized.

I think either way it's the same, personally I like B, :).

It really does boil down to what the situation requires. Functionally there's no difference. One thing you should be careful with, is being sure you have the correct handle to the object you want to modify.

Certainly in this instance, modifying the object is part of the "create the object" thought, and so should be grouped with the constructor as such. After you "create the object" you "add it to the collection". Thus, I would do B, and maybe even add a blank line after the modification to give more emphasis on the two separate thoughts.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions