Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with nitpick in Java-1b

 
Daniel Loranz
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java-1b, I ...

1 - use a loop to fill a string with a full row of the input name.
2 - use a loop to write all the full rows out
3 - write the last partial row, if needed.

Katrina recommended ...
"How about a single loop which counts to 100, printing the name each time?
You can use your namesPerRow variable to decide whether or not you
need to put in a linebreak."

Which I am interpreting as suggesting I use an if-then check inside a single "count-to-100" loop. Would folks agree I'm interpreting this nitpick correctly? If so, I can certainly do that, but I'm lost as to the reason such an approach would be better. Any thoughts??

Thanks very much!

PS - Is this the sort of thing I should have just asked Katrina about in an email reply?
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

The forum is a good place to ask questions like this - just make sure you don't post any actual code, as we don't want others to be robbed of <del>the pain and torture</del> an education!

Yes, you've interpreted the nitpick correctly. There are a couple of reasons why I suggest the approach. The first and foremost is readability.

What are your reservations with respect to the single loop approach?
[ June 29, 2008: Message edited by: Katrina Owen ]
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point is to make code as readable as possible. The bigger your code is, the more there is to read. The art of writing simple, readable code is to write the least amount without sacrificing a clear message. By removing extra loops, you will make your code simpler and more readable, and as an added bonus, your code will be smaller.
 
Daniel Loranz
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize at this scale, any difference in performance will be negligible, but in principle, isn't there a performance issue?

For example, say I enter "Julie" as the arg. In my approach I check for ...
- loop completion 13 times to fill a full row.
- loop completion 7 times to output seven full rows.
- need for a final partial row
- loop completion 8 times to fill out the partial row.

Giving a total of 29 loop checks and 8 invocations of System.out.println

On the other hand the nitpick would lead to 100 loop checks plus 100 if-then checks, plus greater than 100 System.out.print and println executions.

Not trying to be a trouble maker. Just checking. Again, I realize at this scale, any real difference will be negligible, but the readability issue seems at least as marginal in this case.

In the meantime, I'll go ahead and make the suggested change.

Thanks very much.
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your version with three loops, you are concatenating Strings a LOT. For each String concatenation, you are creating (correct me if I'm wrong!) four extra objects.

So you would probably have to run profiling tests on each of the versions before deciding which performs better.

Also, there is the thing about premature optimization: you want to have the most readable code first, then later, if you determine that there is a performance problem, that is when you would consider how to optimize. Often, if you try to optimize too early, you miss some significant opportunities for improvement.
 
Daniel Loranz
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - thanks for that. I keep forgetting about strings being objects.

Also, your point about optimizing too early is well taken. I'll try to keep that in mind.

Thanks!
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Daniel, welcome to the Cattle Drive! Hope you're enjoying the pain an, eh, I mean the learning experience.


[...] but the readability issue seems at least as marginal in this case.


Ah, but this here's the Cattle Drive, pardner. And 'round these parts, readability is a Big Thang. And a Good Thang. The Real Deal. Yessir!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic