Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hint Please

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Received from Nitpick...

Notice how the stuff happening in the if statement is remarkably similar to the stuff in the else statement? I see a huge optimization here - do you?

I am using the println() in the if and the print() in the else. Every loop I am printing with print() and when a line feed is needed I use the println().
I am thinking that the best way to optimize is to move the print out of the loop and durig the loop work with the string to be printed. I can keep adding on to the string until I need a line break "\n", when the loop is finished I can print the new string.
Does this sound like I am on the right track?
------------------
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you printing when you print() and println()? Are they the same/similar?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am printing the same thing every time. I am printing a string (printString) that comes from the command line.
println( printString )
or
print( printStrng )
 
paul wheaton
Trailboss
Pie
Posts: 21736
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you just print( whatever ) and then after that do your if statement where all you do is println().
 
Norman Leach
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
I did what you are recommending in my turn-in. (Only printed actual text in one place and did a println in another). But does that really optimize anything? I mean, what's the difference between doing System.out.print("Norm") 24 times and System.out.println("Norm") once versus System.out.print("Norm") 25 times and System.out.println() once? It seems to me that the second way would actually be LESS optimized because you're making the call to System.out.* one more time that way. Am I just confused about something here?
Norm
 
delwyn allely
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also just submitted assignment 1.1 and have had the pleasure of being nitpicked twice and am looking forward to receiving my 3rd any moment. I am probably not qualified to comment but Pauls suggestion
"What if you just print( whatever ) and then after that do your if statement where all you do is println()."
is close to what I did and I think anything that makes the code simpler and cleaner has got to be better. I feel an conditional statement followed by an if statement is less cluttered than an if-then-else.
It is not just Java expertise being offer in this site, it is good common sense stuff that can be applied to any programming language.
 
paul wheaton
Trailboss
Pie
Posts: 21736
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Delwyn said.
This makes the code more readable, which is the most important thing right now.
What if you change the string, do you want to change the string in one place or two? What if the code grows and the two strings are seperated by a dozen lines? What if you change one and forget to change the other.
Good code will be readable and grow easily in any direction.
 
Daniel Olson
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if it applies to this situation, but sometimes an if else statement can be collapsed into one line using the ternary (conditional) operator.
 
delwyn allely
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel,
Sorry, could you explain this more?
 
Daniel Olson
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a normal if/else such as
if (a > b)
c = a;
else
c = b;
It can be also expressed with a conditional operator
c = (a > b) ? a : b;

Here is an explanation of it from Thinking in Java, by Bruce Eckel
Ternary if-else operator
This operator is unusual because it has three operands. It is truly an operator because it produces a value, unlike the ordinary if-else statement. The expression is of the form:
boolean-exp ? value0 : value1
If boolean-exp evaluates to true, value0 is evaluated and its result becomes the value produced by the operator. If boolean-exp is false, value1 is evaluated and its result becomes the value produced by the operator.
Of course, you could use an ordinary if-else statement, but the ternary operator is much terser.
He also says, "... you should be somewhat wary of using it on an everyday basis�it�s easy to produce unreadable code."
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Wheaton:
This makes the code more readable, which is the most important thing right now.

Originally posted by Daniel Olson:
I'm not sure if it applies to this situation, but sometimes an if else statement can be collapsed into one line using the ternary (conditional) operator.

Here is an explanation of it from Thinking in Java, by Bruce Eckel

Ternary if-else operator

boolean-exp ? value0 : value1

Of course, you could use an ordinary if-else statement, but the ternary operator is much terser.

He also says, "... you should be somewhat wary of using it on an everyday basis it's easy to produce unreadable code."

Yes it exists. Yes it is occasionally useful. However, it doesn't apply in this situation.

[This message has been edited by Marilyn deQueiroz (edited January 16, 2001).]
 
Chuck Lalli
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although the ternary form is valid in some cases, it does produce code that is hard to read. If I have to pause and think " How does that work again?" when maintaining code then I am more likely to make a mistake. Also, it is for assignments, not for statements.
 
delwyn allely
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for explaining this, I need to know what it is,
but coming from a VB background, and further back again Cobol
I think it is YUCKY!
 
Daniel Olson
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll accept what our teacher said about the conditional operator, "it doesn't apply in this situation."
However, I think the Eckel quote, "it's easy to produce unreadable code" doesn't mean that the conditional operator is unreadable. It just needs to be used with care. It might be unfamiliar to some people, but it can be a clear way to write code in some cases. After using it a few times, a programmer can understand it just as quickly as an if-else statement.
 
paul wheaton
Trailboss
Pie
Posts: 21736
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are cases where it adds readability. Usually, this involves doing the boolean test beforehand:
boolean done = ( ( goats > 100 ) && ( cows > 50 ) ) ;
System.out.println( done ? "all done!" : "working..." );
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic