开发者

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

开发者 https://www.devze.com 2023-01-09 03:51 出处:网络
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 befor

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> obje开发者_如何学编程cts) {
    MyObject newObject = new MyObject();
    newObject.setMyAttr("ok");
    objects.add(newObject);
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消