OOP: passing an object or passing attributes to a method

As for clean design, which is preferable?

1) Passing an object, say, to another method of the class that will directly manipulate the attributes of the passed object.

Inside class A ...

B.doStuff(this);

2) Instead, pass the attributes of the object and either assign a return back to the attributes, or pass by reference.

Inside class A ...

this.var1 = B.doStuff(this.var1);

The disadvantage of the first method is the ambiguity, it is not clear that B changes to A. The disadvantage of the second method is that if the attributes are not passed using references / pointers, then you need to return an array, and it will also be a longer function call if you finish passing the set attributes. I assume that the right choice depends on the situation, but are there any other advantages / disadvantages that anyone else can come up with before deciding?

: , ( ), , . , , , B A, B , A . , , , , .

+3
3

, , . .

, , .

, , , .

, Graphic Paint Forms ( #), , (, , ).

, . , , , , , , , .

, , , , , , , .

, , , , - , Data , , , , . - , , - , .

+1

. . ORM, Hibernate, - , : -

public void addChild(Child child) {
    children.add(child);
    child.setParent(this);
}

... , (), , . , , API, .

+1

I would prefer the second approach, then class B will not have a dependency on class A.

+1
source

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


All Articles