This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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? ------------------
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
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.
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.
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.
Joined: Jan 04, 2001
Daniel, Sorry, could you explain this more?
Joined: Jan 05, 2001
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
Joined: Jul 22, 2000
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).]
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.
Joined: Jan 04, 2001
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!
Joined: Jan 05, 2001
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.
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..." );