*
The moose likes Cattle Drive and the fly likes Not sure what this nit pick means here? 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 "Not sure what this nit pick means here?" Watch "Not sure what this nit pick means here?" New topic
Author

Not sure what this nit pick means here?

Trevor Cole
Greenhorn

Joined: May 02, 2006
Posts: 5
Hi Guys,

I have a nitpick back which stated the following:

>> if ( (charsDisplayedCount += name.length()) > 80 )
>Rather than doing all of this massive and expensive string concatenation,
>wouldn't it be easier to do some simple math? I suggest that you
>calculate the number of iterations ahead of time.

Surely I'm *NOT* doing a String concatenation as the length() method of the String class simply returns an int?

Am I missing something here? If I do has suggested, my code seems to grow by another 5 lines and would be more complex !!

Regards,


Trevor
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

are you doing string concatenation somewhere else? in my final solution, the program would do exactly one concatenation. perhaps the comment isn't referring to this exact line, but your more general program flow...


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Trevor,

I hesitate to try to interpret a nitpick out of context, but I wonder if maybe it refers to the calcalation as opposed to a concatenation. (Some of this nitpicking business gets done at less than ideal times of day, and, well, you know how that can go.)

Is that particular line inside a loop? If you can, it's usually a good idea to pull a calculation out of a loop for optimization reasons.


Looks like you're on Java 1b, and that one can be a real stickler in disguise. Hang in there!

Pauline
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Yep. I meant calculation rather than concatenation. Sorry to mislead you.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Adding integers is considered "massive and expensive" now? As opposed to "simple math"? I suppose in context this advice might make more sense, but on the surface it seems... odd.


"I'm not back." - Bill Harding, Twister
Trevor Cole
Greenhorn

Joined: May 02, 2006
Posts: 5
Thanks guys,

The explanations above now make sense, although it does look like good code, 3 colleagues at work all preferred the original code over the latest with 'Simple Maths' as it was easier to understand.

However doing the calculation in the for loop seems like it could be costly on the cpu and there is another less costly way of doing it.

Something I've never thought about before and I've been coding in other languages for 16 years.....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ah, now that I see what assignment he's on this makes a bit more sense. Still... "massive and expensive"?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Ok. I confess.
I didn't read it carefully enough. "massive and expensive" is definitely an exaggeration. However, I still think there is a better way. And we'll find it, Trevor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not sure what this nit pick means here?
 
Similar Threads
performance Issue
System.out.print vs. String concantenation
appending StringBuffer to String
Why does 1b seem so freakin tricky to me.
Output based on multiple combinations