This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Hint Please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Hint Please" Watch "Hint Please" New topic
Author

Hint Please

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
What are you printing when you print() and println()? Are they the same/similar?

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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Dec 14, 1998
Posts: 20494
    ∞

What if you just print( whatever ) and then after that do your if statement where all you do is println().


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Norman Leach
Greenhorn

Joined: Jan 05, 2001
Posts: 17
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

Joined: Jan 04, 2001
Posts: 4
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

Joined: Dec 14, 1998
Posts: 20494
    ∞

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

Joined: Jan 05, 2001
Posts: 81
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

Joined: Jan 04, 2001
Posts: 4
Daniel,
Sorry, could you explain this more?
Daniel Olson
Ranch Hand

Joined: Jan 05, 2001
Posts: 81
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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

Joined: Jan 06, 2001
Posts: 12
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

Joined: Jan 04, 2001
Posts: 4

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

Joined: Jan 05, 2001
Posts: 81
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

Joined: Dec 14, 1998
Posts: 20494
    ∞

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..." );
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hint Please
 
Similar Threads
Need help with HashMap object mapping-- type mismatch how to fix?
system.out.print vs println
Doubt with ++ operator
One more question on garbage collection
Optimize - Assignment 1