tags:

views:

192

answers:

6

Suppose Object1 needs information from Object2. I'll say it's in an Object2 property, but the info could as easily be the return value from an Object2 function. When I look at others' code I see sometimes they will have a method in Object1 directly accessing the property. Other times I see people pass Object2 as a parameter in a method, and then access the property from the passed Object2.

These scenarios seem almost the same to me. Directly accessing the property seems simpler. As a newbie, what do you think I should think about when deciding how Object1 should get information from Object2? (When would I want to have an object parameter rather than directly accessing the property?)

Thanks -- Al C.

+7  A: 

One problem with passing Object2 to Object1 is that you create a dependency between Object2 and Object1. The only way that Object1 can obtain the data it needs is to have a reference to Object2.

Now, sometimes you want that, but most of the time you don't. So you're most likely better off simply passing the value you need as a parameter to the method, rather than passing the instance of Object2.

Randolpho
+1  A: 

It's all about whether or not Object1 knows about Object2. In OOP, part of good design is to have objects store as LITTLE information as possible that still allows them to behave properly. So the problem fundamentally comes down to: does Object1 need to know about Object2's existence (that is, does it need an instance of Object2 to exist) or can it be told about the existence of an Object2 instance? Usually, in OOP, you want to lean more towards the latter than the former.

However, that said, in a situation where Object1 can operate on data which comes from Object2, the best way would probably to have Object1 have a method which operates on the data type of the property on Object2; that way, you decouple Object1 and Object2, in that Object1 does not have a dependency on Object2 to provide a given property in a given way; the calling code can connect the two.

McWafflestix
I'm a little confused about how Object1 could be told about Object2 without knowing about Object2's existence.
Al C
@AlC: think dependency injection.
McWafflestix
+1  A: 

Yes, what Randolpho says is true.

Remember, with OOP you are aiming to create separate identities that can function as a whole in the system. When you start creating objects that rely heavily on each other, not only do you increase your chances of bugs, but also the chance that your program won't work anymore at all.

Now your example is a minor, but imagine 5, 10, 100 objects calling each other through parameters, that's nasty work.

Jaime
+1  A: 

If the method only needs the one property value from Object2, it would be best to pass that property value directly. If the method needs access to many properties or other features of Object2, passing a reference to Object2 would be appropriate.

There are exceptions - if you need to protect the signature of the method (i.e. it is public) and you foresee additional use of Object2 within the method in the future, passing the object reference may be appropriate.

The previous answers also make valid points - you should consider the coupling of Object1 and Object 2 when you make this decision.

Scott Ewers
+1  A: 

Take a look at Encapsulation on wikipedia.

Encapsulation [..] protects the integrity of the component, by preventing users from setting the internal data of the component into an invalid or inconsistent state.

toolkit
+2  A: 

Usually the goal is to minimize coupling between objects.

Let's use a simple example where I have three objects; an AppController, which holds a reference to a DataController and a MainWindowController. You need to pass DataController.Data to the MainWindowController, so you can display the values in a table in the main window. You could pass DataController to MainWindowController as a method parameter, or make DataController an instance variable in MainWindowController. To minimize coupling through, the cleanest way is for AppController to pass Data directly to MainWindowController, so it doesn't need to know anything about DataController.

Marc Charbonneau