• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Coding Style - which one is preferable?

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
While passing parameters to methods, which style is preferable??
ClassType1 obj1 = new ClassType1();
RtnType1 value1 = obj1.someMethod();
ClassType2 obj2;
RtnType2 value2 = obj2.anotherMethod(value1);
OR
RtnType2 value2 = obj2.anotherMethod(
new ClassType1().someMethod());
I feel the second one is preferable. But I wish to know your opinion.
Thanks
Padmaja
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
it depends if the object has a scope and serves a purpose for more then 1 method calls declare and use .. if not there is no purpose to declare and use it just use it as an anonymous object will be used .. for example even delegation where the method and object just comes alive for tht particular moment
hope this helps ...
cheers,
parthi.
 
Padmaja Prasad
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Parthi, That really helps..
Thanks
Padmaja
 
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,
While passing parameters to methods, which style is preferable??
ClassType1 obj1 = new ClassType1();
RtnType1 value1 = obj1.someMethod();
ClassType2 obj2;
RtnType2 value2 = obj2.anotherMethod(value1);
OR
RtnType2 value2 = obj2.anotherMethod(
new ClassType1().someMethod());
I feel the second one is preferable. But I wish to know your opinion.


My vote goes to method #1, as it is much more readable. However, if the parameter were just a new object, I would do it as in method #2, because it is still clear, yet concise:
RtnType2 value2 = obj2.anotherMethod(
new ClassType1(someValue));
Eugene.
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Padmaja,
I agree with Eugene. Method 1 is much easier to read, and potentially to debug. Also, it's no more expensive then method two.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with both Max and Eugene. Besides, Method #2 violates the Law of Demeter: Friends should only talk to friends.
Michael Morris
 
Padmaja Prasad
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys,
I thought #1 will have to allocate memory for the extra variables declared.

Friends should only talk to friends.


Michael, I like this explanation.
Thanks
Padmaja
 
parthiban subramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
yeah i agree it makes life easy to debug for example one might throw a null pointer error and u can just grab the line which throwed it in the case 1 but are'nt we making too many declarations ??
and there is no use for those dclarations after that line why cant we just create an anonymous object in the place of use ??
cheers,
parthi.
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is actually a pet peeve of mine, so I'll add my two cents.
in short, no, we are not. What you are creating is an extra reference, which is so cheap as to be insignificant. What does this mean?
For example, say you have the following
line 1: String tmp = "Hello";
line 2: System.out.println(tmp);
vs
line 3: System.out.println("Hello");
Line 3 create a new String variable, just as line 1 does. The only difference between the is creating of the reference 'tmp'.
However, 'tmp' is not a new variable, it's just a reference. In effect, it's just a int, large enough to point to the memory location of the String "Hello". Thus, no real overhead, but a lot more clarity.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Padmaja Prasad
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Max, That clears my doubt.
Thanks
Padmaja
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad to help :-)
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with those that agreed.
Mark
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I would like to agree that Mark has agreed to agree.
 
Max Habibi
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ditto

M, author
The Sun Certified Java Developer Exam with J2SE 1.4
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Ditto


To quote a famous cartoon character:
Monotonous, isn't it?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic