File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Coding Style - which one is preferable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Coding Style - which one is preferable?" Watch "Coding Style - which one is preferable?" New topic
Author

Coding Style - which one is preferable?

Padmaja Prasad
Ranch Hand

Joined: Nov 14, 2001
Posts: 76
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
parthiban subramaniam
Ranch Hand

Joined: May 15, 2002
Posts: 116
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.


Even crazy and silly looking problems are sometimes real.
Padmaja Prasad
Ranch Hand

Joined: Nov 14, 2001
Posts: 76
Thanks Parthi, That really helps..
Thanks
Padmaja
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

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.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
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


Java Regular Expressions
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
I agree with both Max and Eugene. Besides, Method #2 violates the Law of Demeter: Friends should only talk to friends.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Padmaja Prasad
Ranch Hand

Joined: Nov 14, 2001
Posts: 76
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

Joined: May 15, 2002
Posts: 116
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)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
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

Joined: Nov 14, 2001
Posts: 76
Thanks Max, That clears my doubt.
Thanks
Padmaja
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Glad to help :-)
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I agree with those that agreed.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
And I would like to agree that Mark has agreed to agree.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Ditto

M, author
The Sun Certified Java Developer Exam with J2SE 1.4
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

Ditto

To quote a famous cartoon character:
Monotonous, isn't it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coding Style - which one is preferable?