Do I need to return multiple objects to a bad design sign?

Sometimes I come across this situation in Java, trying to use immutable objects. In Java, a method cannot return multiple objects, for example return a, b, c. Returning an array of objects of different types or a wrapper object makes the code look ugly. Therefore, I must pass mutable objects to the method and let it change the state of these objects. So generally speaking, when programming you have to return several objects to a bad design sign?

+3
source share
4 answers

No, I do not believe. The ability to return tuples in Python was really convenient.

, , - (, /- / ), .

, , , , - . API , , , .

- "", ( , , ). , , , , .

+2

. - "OperationResult", .

pass-an-in-and-fill-out-its-members. .

+3

, , , imho, "", .

, , .

0

Java , wrapper object, , Transfer Object, .

Modifying parameters are also a very common method. You can see examples in the JDK itself, for example Collections.sort (List of lists) or InputStream.read (byte []) .

It is also common when working with persistent objects, for example, in the code snippet below

public void updateTrackingFields(Trackable trackable, String user) {
  trackable.setChangedBy(user);
  trackable.setChangedTime(System.currentTimeMillis());
}

So, I believe that there can be much worse design decisions than these two.

EDIT: Added an example of persistent objects.

0
source

Source: https://habr.com/ru/post/1774001/


All Articles