2

Say you are adding x number of objects to a collection, and after or before adding them to a collection you are modifying the objects attributes. When would you add the element to the collection before or after the object has been modified.

Option A)

public static void addToCollection(List<MyObject> objects) {
    MyObject newObject = new MyObject();
    objects.add(newObject);
    newObject.setMyAttr("ok");
}

Option B)

public static void addToCollection(List<MyObject> objects) {
    MyObject newObject = new MyObject();
    newObject.setMyAttr("ok");
    objects.add(newObject);
}
2
  • I don't know, I am just wondering if there is a best practice for this. Jul 26, 2010 at 21:55
  • 1
    for certain types of collections, it does matter.
    – user8681
    Jul 26, 2010 at 22:04

7 Answers 7

12

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.

6
  • I think you mean in general, modify the object first? Jul 26, 2010 at 21:59
  • Yes, I mean that. Sorry, I have a cold. :)
    – mdma
    Jul 26, 2010 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, 2010 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). Jul 26, 2010 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 27, 2010 at 0:17
4

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.

1

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.

0

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.

0

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

0
0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.