• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Not sure what this nit pick means here?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep. I meant calculation rather than concatenation. Sorry to mislead you.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Trevor Cole
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, now that I see what assignment he's on this makes a bit more sense. Still... "massive and expensive"?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic