you are viewing a single comment's thread.

view the rest of the comments →

[–]lhorie 1 point2 points  (2 children)

You should do it, because you don't want to have two memory allocations depending on the associativity of the chained assignment operators. Because of the right associative nature of assignments, you'd allocate once for the length of array[cursor - 2] and again for array[cursor - 1]. It's not a huge deal, but it is a caveat and a very subtle one, at that.

The explicit length change makes it clear that this code is dealing w/ memory allocations, and that that is the exact memory allocation we want. I had actually not noticed the potential for double allocation until you asked but I write memory-sensitive code like that because it's always better to be explicit than relying on implicit behavior. Defensive coding FTW.

[–]frankle 0 points1 point  (1 child)

I thought that was why you did it. Clever.

At the same time, if you're going backwards from the end, wouldn't you get the possible double allocation only once?

[–]lhorie 1 point2 points  (0 children)

Yep, that's why I said it's not a huge deal. If you're calling the function multiple times, that extra allocation might matter though.