Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

direct/indirect References in Objects

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where lies the difference between this:



and this:



In both cases, the mouse-property of name is set in Object Cat. Which one is better? differences?
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is there in both classes?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Work this out with a pencil and paper: What is the Mouse object in the cat before and after each bit of code? Then you can see whether there is a significant difference or not. And by "w" did you mean "m"?

Some people like lots of invocations joined together by dots, some don't. It depends on what the methods return; if they return an object reference, you can use join-the-dots invocations, if they are void you can't.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, yes I mean m instead of w.

In both cases, the Mouse-Object of Cat are changed.

This is obvious:



But this not:



So I guess, I prefer the direct way:



It s clearer.
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:hello, yes I mean m instead of w.

In both cases, the Mouse-Object of Cat are changed.

This is obvious:



But this not:



So I guess, I prefer the direct way:



It s clearer.


it seems to me that
if we need value getting from Cat object,


if we need to insert new/different value than Cat object,


i think no change in performance if values to be set is equal as


am i rightly thinking?

 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure; I think you need to try that code out in different contexts. I am not sure "direct" and "indirect" are quite the right words to use.

I think what you have here are "aliases": two names for the same object. You can say m.setName("Jerry") or c.getMouse().setName("Jerry").
You will have to try different combinations, and m.getName() or c.getMouse().getName(). I think you will find that m.setName("Jerry") followed by c.getMouse().getName() will return "Jerry".
Getting two different names for the same object can cause no end of confusion because you forget that calling a method on m also affects c.getMouse().

I think it is a case of what is easier to read, and I don't think there will be a performance difference.
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I think it is a case of what is easier to read, and I don't think there will be a performance difference.

+1
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think it is a case of what is easier to read, and I don't think there will be a performance difference.


nimo asked for difference

for sure, is more readable than

but, is there any performance related difference if 'm' is used instead of


 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is a case of what is easier to read, and I don't think there will be a performance difference.




and I don't think there will be a performance difference.


I hope so:-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic