Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Not sure what this nit pick means here?

 
Trevor Cole
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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,
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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...
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • 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
 
Marilyn de Queiroz
Sheriff
Posts: 9064
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. I meant calculation rather than concatenation. Sorry to mislead you.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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: 9064
12
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic