Skip to content

Object is modified implicitly in one method and used by another

I have two methods meth1 and meth2. meth1 takes in an input of a set set1, returns a value, and also modifies set1.

Now meth2 uses this set1 to compute another return value.

The issue I see with this code is that the set modification is implicit and would be missed by a coder who is not careful enough.

I could do all the operations in a single method, but the working of both the methods is different enough to put them in different methods. Also a single method would be very long.

So given that I have to use two methods here, what is the best way to avoid the implicit modifications? What is the best practise to deal with this situation?

Any pointers are highly appreciated.

Method signatures:


private List<CustomObject> meth1(List<CustomObjectOnWhichOperationIsPerformed> lst,
Set<AnotherCustomObject> set1, Map<String, AnotherCustomObject> mp) {
// Iterate on lst, call mp and remove AnotherCustomObject entities from set1


private List<CustomObject> meth2(Set<AnotherCustomObject> set1) {
// Iterate on set1 to create the list



Can this problem be approached differently? It looks like a violation of the single responsibility principle. The first method returns a value but also modifies the argument passed to it.

Break the first method up into two methods

  1. Modify the set (explicitly)
  2. handle the logic for getting the first return value

Then the last step

  1. compute another return value