my dog learned polymorphism*
The moose likes Cattle Drive and the fly likes one computation to be printed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "one computation to be printed" Watch "one computation to be printed" New topic
Author

one computation to be printed

Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
Say you have to print the sum of two numbers A + B (gotten by chance ;-) )
If done, the sum is not neaded anymore.
Two versions:
int sum = A + B ;
System.out.println( "the sum is: " + sum );
Second version
System.out.println("the sum is: " + ( A + B ) );
What do you prefere?
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Considering the complexity (or rather the lack of it), I dont have a preference either way.
Joel Cochran
Ranch Hand

Joined: Mar 23, 2001
Posts: 301
One of the first nitpicks I got was creating a variable that was only used once, so it would seem to me that the Second version would be preferred.

------------------
I'm a soldier in the NetScape Wars...
Joel


Wait a minute, I'm trying to think of something clever to say...<p>Joel
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Sharp Joel sharp, I should have caught Peter's topic.
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
Ja, ja.
This question has its origin in comparing the 'nitpickers' solution to mine.
But I doubt mine, because 'clarity' comes before efficiency?!
Mayby Marilyn gives her meaning too?
I am referring to DaysOld:
System.out.println( sorry Peter I feel I had to remove the code here );
For Johannes:
I get a notification from your reply TWICE, yesterday too. You know the reason? Do you know to eliminate this 'feature' ?!

[This message has been edited by Peter Gragert (edited May 18, 2001).]
[This message has been edited by Johannes de Jong (edited May 18, 2001).]
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
I replied twice Peter
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
If the sum is not needed again, then use the second one. Saves lines, and looks "more advanced"

/Mike
[This message has been edited by Mikael Jonasson (edited May 18, 2001).]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

If it is only going to be used once, chances are that you should eliminated it. For the sake of clarity.
But! There are many times that introducing an identifier adds to the clarity - in which case the extra identifier should be kept.
For an example, see http://www.javaranch.com/style.jsp#space


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Since Paul is the author of the solution, I thought it would be appropriate for him to comment. I thought you answered your own question, Peter, when you said, "because 'clarity' comes before efficiency." Readability is the most important thing, but sometimes the code is simple enough that an identifier, rather than clarifying, merely adds another line to read.

So it seems that the answer is, "it depends" on how simple or complex the code is. When it is complex, then an identifier is helpful. Otherwise, it is probably not helpful, but extraneous.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
I think I agree to Marily en Paul. Thanks for the comments !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one computation to be printed
 
Similar Threads
question about using boolean to check a condition of an object?
doubt
Performance!!
Scanner class help
Aggregate functions problem in jsp