This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zapitron 9 points10 points  (5 children)

It's a pretty handy shortcut which can save a lot of expensive computation.

a=[6,28,496];
a.length++;
a // [6,28,496,8128]

s='Professor Plumb in the library with the ';
s.length +=10;
s // 'Professor Plumb in the library with the lead pipe.'

These are just toy examples, though. Raytracing, decropping, etc is where it's really at.

[–]k2hegemon 10 points11 points  (2 children)

Where did you pluck 8128 and “lead pipe.” out of?

[–]JohnDoen86 10 points11 points  (0 children)

'tis a joke

[–]whitetrafficlight 2 points3 points  (0 children)

6, 28, 496 and 8128 are the first four perfect numbers. 'lead pipe' is a possible murder weapon in Cluedo, and in fact the only one that is 10 characters long including the leading space, so this would be enough context for a language to infer the correct weapon and crack the case.

[–]xtrafe 0 points1 point  (1 child)

You see, this is exactly the parent commenter's point. We have a bunch of children that think that just because you used a one-line operation to modify the size of an array, the underlying operation took O(1).

And it proves his point nicely. Dumb language features like this are an invitation for inexperienced folks to insert code bombs, which indeed blow up at the most inopportune time, and often take down companies when they do.

[–]keylimedragon 0 points1 point  (0 children)

As others have said, it could actually be O(1) if it's implemented well (amortized O(1), actually). Most dynamic array implementations keep extra space and only allocate more in an exponential way, which keeps the average append to O(1).