views:

122

answers:

7

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);
}
A: 

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.

Vash
A: 

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

eiefai
+4  A: 

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.

David Young
+12  A: 

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.

mdma
I think you mean in general, modify the object first?
David Young
Yes, I mean that. Sorry, I have a cold. :)
mdma
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
@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
@ 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
@bwawok: I'm sorry but you're wrong (and it looks like you never used an ORM).
Pascal Thivent
A: 

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.

Mchl
A: 

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.

ILMTitan
+1  A: 

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.

Alexander Pogrebnyak