• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one computation to be printed

 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering the complexity (or rather the lack of it), I dont have a preference either way.
 
Joel Cochran
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharp Joel sharp, I should have caught Peter's topic.
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I replied twice Peter
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 21473
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I agree to Marily en Paul. Thanks for the comments !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic