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?
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
Joined: Jan 27, 2001
Sharp Joel sharp, I should have caught Peter's topic.
Joined: Jan 16, 2001
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).]
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
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
Joined: Jan 16, 2001
I think I agree to Marily en Paul. Thanks for the comments !